当前位置: 首页 > news >正文

产品做推广一般上什么网站seo管理系统创作

产品做推广一般上什么网站,seo管理系统创作,分享wordpress小程序源码,如何做网站霸屏文章目录 一、urllib1. urlparse 实现 URL 的识别和分段2. urlunparse 用于构造 URL3. urljoin 用于两个链接的拼接4. urlencode 将 params 字典序列化为 params 字符串5. parse_qs 和 parse_qsl 用于将 params 字符串反序列化为 params 字典或列表6. quote 和 unquote 对 URL的…

文章目录

  • 一、urllib
    • 1. urlparse 实现 URL 的识别和分段
    • 2. urlunparse 用于构造 URL
    • 3. urljoin 用于两个链接的拼接
    • 4. urlencode 将 params 字典序列化为 params 字符串
    • 5. parse_qs 和 parse_qsl 用于将 params 字符串反序列化为 params 字典或列表
    • 6. quote 和 unquote 对 URL的中文字符进行编码和解码
  • 二、requests
    • 1. GET 请求
    • 2. POST 请求
    • 3. Session 维持
    • 4. 响应
    • 5. 身份认证
    • 6. 设置代理
    • 7. Prepared Request
  • 三、httpx
    • 1. Requests Compatibility
    • 2. 异步
  • 四、基础爬虫实战

一、urllib

urllib 类似于 python 底层构建请求,构建相对于其他的库来说较为复杂,不过 urllib 解析链接非常好用

1. urlparse 实现 URL 的识别和分段

如果需要合并 params 和 query 可以使用 urlsplit

from urllib.parse import urlparseurlparse('https://www.baidu.com/')# ParseResult(scheme='https', netloc='[www.baidu.com](https://www.baidu.com/)', path='/', params='', query='', fragment='')

2. urlunparse 用于构造 URL

urlunparse 这个方法接收的参数是一个可迭代对象,且其长度必须为 6;同样的,如果需要合并 params 和 query 可以使用 urlunsplit

from urllib.parse import urlunparsedata = ['https','www.baidu.com','index.html','user','a=6','comment']urlunparse(data)
# https://www.baidu.com/index.html;user?a=6#comment

3. urljoin 用于两个链接的拼接

urljoin 首先会解析 new_url,判断其 scheme,netloc,path 是否出现了缺失,如果确实使用 base_url 中的 scheme,netloc,path 对应缺失部分代替;

from urllib.parse import urljoinbase_url = 'https://www.baidu.com'
new_url = 'FAQ.html'urljoin(base_url, new_url)
# https://www.baidu.com/FAQ.html

4. urlencode 将 params 字典序列化为 params 字符串

from urllib.parse import urlencodeparams = {'name': 'germey','age': 2,
}base_url = 'https://www.baidu.com?'
base_url + urlencode(params)
# https://www.baidu.com?name=germey&age=2

5. parse_qs 和 parse_qsl 用于将 params 字符串反序列化为 params 字典或列表

from urllib.parse import parse_qs, parse_qslparams = 'name=germey&age=25'parse_qs(params, separator='&')
# {'name': ['germey'], 'age': ['25']}parse_qsl(params, separator='&')
[('name', 'germey'), ('age', '25')]

6. quote 和 unquote 对 URL的中文字符进行编码和解码

from urllib.parse import quote, unquoteurl = "https://www.baidu.com/s?wd=爬虫"# utf8编码,指定安全字符
quote(url, safe=";/?:@&=+$,", encoding="utf-8")
# https://www.baidu.com/s?wd=%E7%88%AC%E8%99%AB# gbk编码,指定安全字符
quote(url, safe=";/?:@&=+$,", encoding="gbk")
# https://www.baidu.com/s?wd=%C5%C0%B3%E6# utf8解码
unquote('https://www.baidu.com/s?wd=%E7%88%AC%E8%99%AB', encoding='utf-8')
# https://www.baidu.com/s?wd=爬虫# gbk解码
unquote('https://www.baidu.com/s?wd=%C5%C0%B3%E6', encoding='gbk')
# https://www.baidu.com/s?wd=爬虫

二、requests

1. GET 请求

测试 URL: www.httpbin.org/get

cookies 可以单独设置,也可以放在 headers 的 cookie 字段下传入请求之中,timeout 可以控制超时时间,headers 是请求头,params 是参数构建完整的 url;

import requestsparams = {}
headers = {}
cookies = {}# vertify 设置为 False 可以避免 ssl 认证
requests.get(url=url, params=params, headers=headers, cookies=cookies, vertify=False, timeout=None)

2. POST 请求

测试 URL: www.httpbin.org/post

POST 是上传东西的常用请求,POST 请求中除了 GET 请求中的那些参数,还有一些参数可以使用,如 data 和 file;其中 data 主要用来传表单,而 file 主要用来传文件;

import requestsparams = {}
headers = {}
cookies = {}data = {}
file = {}# vertify 设置为 False 可以避免 ssl 认证
requests.post(url=url, params=params, headers=headers, cookies=cookies, vertify=False, timeout=None, data=data, file=file)

3. Session 维持

多次直接利用 requests 库中的 get 或 post 方法模拟网络请求,相当于打开了多个不同的浏览器,而使用 Session 维持 搭配 get 和 post 方法去模拟网络请求,相当于打开了一个浏览器中的多个页面;

import requestss = requests.Session(headers=headers)
s.get(url_1)
s.get(url_2)# 这里在第一次 get 请求中获得到的 cookie 就会保持进而在第二次 get 请求中得到

4. 响应

import requestsresp = requests.get()# 状态码
resp.status_code# 响应头
resp.headers# cookies
resp.cookies# 最终 url 搭配重定向使用 requests.get(url, allow_redirects=False)
resp.url# 请求历史
resp.history# 在获取 resp.text 先配置 encoding
resp.encoding# 响应结果字符串形式,需要搭配 resp.encoding = 'utf-8' or 'gbk' 使用
resp.text# 二进制相应结果,通常对应于文件
resp.content# resp.text 转化为 json 数据,如果不是json 数据格式,则会出现解析错误,抛出 json.decoder.JSONDecodeError 异常
resp.json

5. 身份认证

在访问启用了基本身份认证的网站时,首先会弹出一个认证窗口,认证正确会弹出 200状态码,如果认证错误或者不进行认证会弹出 401 错误;

import requests
from requests.auth import HTTPBasicAuth# 第一行是第二行的简写
r = requests.get('https://ssr3.scrape.center/',auth=('admin','admin'))
r = requests.get('https://ssr3.scrape.center/',auth=HTTPBasicAuth('admin','admin'))r.status_code
# 200

requests 库还提供了其他的认证方式,如 OAuth 认证,需要安装 oauth 包;

6. 设置代理

首先是基本的 HTTP 代理

import requestsproxies = {'http':'http://10.10.10,10:1080','https':'https://10.10.10.10:1080',
}requests.get('https://www.httpbin.org/get', proxies=proxies)

除了基本的 HTTP 代理外,还支持 SOCKS 协议的代理,首先需要安装 socks 库 pip install requests[socks]

import requestsproxies = {'http':'socks5://10.10.10,10:1080','https':'socks5://10.10.10.10:1080',
}requests.get('https://www.httpbin.org/get', proxies=proxies)

7. Prepared Request

因此多个 get 或者 post 请求相当于多个 Session,尽量避免对同一网页使用多个 get 或者 post 请求

from requests import Request,Sessionurl = 'https://www.httpbin.org/post'
data = {'name':'germey'}
headers = {}# 请求的底层
s = Session()
req = Request('POST', url, data=data, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped)# 等价
r = requests.post(url, data=data, headers=headers)

三、httpx

HTTPX 建立在 requests 完善的可用性之上,支持 HTTP/2 并支持异步;HTTPX (python-httpx.org)

可选安装如下:

  • h2 - HTTP/2 支持。 (可选,带有 httpx[http2]
  • socksio - SOCKS 代理支持。 (可选,带有 httpx[socks]
  • brotlibrotlicffi - 解码“brotli”压缩响应。 (可选,带有 httpx[brotli]

HTTPX 与 requests 的 API 广泛兼容,在少部分地方存在一些设计差异:Requests Compatibility - HTTPX (python-httpx.org)

1. Requests Compatibility

重定向:与 requests 不同,HTTPX 默认情况下是不遵循 重定向 (redirects) 的,开启重定向如下所示

client = httpx.Client(follow_redirects=True)
response = client.get(url, follow_redirects=True)

Client:等价于 requests.Session 维持,即等价

session = requests.Session(**kwargs)
client = httpx.Client(**kwargs)

URL:访问 response.url 将返回 url 实例,requests 返回的是字符串

重定向请求requests 库公开了一个属性 response.next ,该属性可用于获取下一个重定向请求。在 HTTPX 中,此属性被命名为 response.next_request

# requests
session = requests.Session()
request = requests.Request("GET", ...).prepare()
while request is not None:response = session.send(request, allow_redirects=False)request = response.next# httpx
client = httpx.Client()
request = client.build_request("GET", ...)
while request is not None:response = client.send(request)request = response.next_request

请求内容:对于上传原始文本或二进制内容,httpx 使用 content 参数,以便更好地将这种用法与上传表单数据的情况分开。使用 content=... 上传原始内容,并使用 data=... 发送表单数据;

httpx.post(..., content=b"Hello, world")
httpx.post(..., data={"message": "Hello, world"})

上传文件:HTTPX 严格强制上传文件必须以二进制模式打开,以避免尝试上传以文本模式打开的文件可能导致的字符编码问题。

内容编码:HTTPX 使用 utf-8 来编码 str 请求正文。例如,当使用 content=<str> 时,请求正文将在通过线路发送之前编码为 utf-8

Cookietrust_envverifycert 参数:如果使用客户端实例,应始终在客户端实例化时传递,而不是传递给请求方法。

2. 异步

requests 是不支持异步的,通常我们会使用 aiohttp 来进行异步操作,而 httpx 不仅支持同步还支持异步

import asyncio
import httpxasync def main():async with httpx.AsyncClient() as client:response = await client.get('https://www.example.com/')print(response)asyncio.run(main())

四、基础爬虫实战

任务:

  1. 使用爬虫基本库爬取 https://ssr1.scrape.center/ 每一页的电影列表,顺着列表再爬取每个电影的详细页
  2. 使用正则表达式提取每部电影的名称,封面,类别,上映时间,剧情简介等内容;
  3. 使用多进程实现爬取的加速;

流程:

代码:

import os
import re
import httpx
import json
from multiprocessing import Pool
from urllib.parse import urljoinbase_url = "https://ssr1.scrape.center"def scrape_index(page):"""获得page的url"""page_url = f"{base_url}/page/{page}"return page_urldef scrape_list(html):"""获得列表的url"""url_list = re.findall(r'<a data.* href="(.*)" class="name">', html)url_list = [urljoin(base_url, item) for item in url_list]return url_listdef scrape_detail(html):"""获得详细页信息"""detail_dic = {}detail_dic["名称"] = (re.search(r'<h2 data.*? class="m-b-sm">(.*?)</h2>', html, re.S).group(1)if re.search(r'<h2 data.*? class="m-b-sm">(.*?)</h2>', html, re.S)else None)detail_dic["封面"] = (re.search(r'class="item.*?<img.*?src="(.*?)".*?class="cover">', html, re.S).group(1)if re.search(r'class="item.*?<img.*?src="(.*?)".*?class="cover">', html, re.S)else None)detail_dic["类别"] = re.findall(r"<button.*?category.*?<span>(.*?)</span>.*?</button>", html, re.S)detail_dic["上映时间"] = (re.search(r"<span>.*?(\d{4}-\d{2}-\d{2}) 上映", html, re.S).group(1)if re.search(r"<span>.*?(\d{4}-\d{2}-\d{2}) 上映", html, re.S)else None)detail_dic["剧情简介"] = (re.search(r"剧情简介</h3>.*?<p.*?>(.*?)</p>", html, re.S).group(1).strip()if re.search(r"剧情简介</h3>.*?<p.*?>(.*?)</p>", html, re.S)else None)return detail_dicdef validateTitle(title):"""命名规范性"""rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'new_title = re.sub(rstr, "_", title)  # 替换为下划线return new_titledef save_json(detail_dic):"""保存数据到json文件夹下"""os.makedirs("./json", exist_ok=True)name = detail_dic["名称"]data_path = f"./json/{validateTitle(name)}.json"json.dump(detail_dic, open(data_path, "w", encoding="utf-8"), ensure_ascii=False, indent=2)def main(page):client = httpx.Client()page_url = scrape_index(page)resp_page = client.get(page_url).texturl_list = scrape_list(resp_page)for detail_url in url_list:resp_detail = client.get(detail_url).textdetail_dic = scrape_detail(resp_detail)save_json(detail_dic)if __name__ == "__main__":pool = Pool(10)pages = range(1, 10 + 1)pool.map(main, pages)pool.close()pool.join()

得到结果如下:


文章转载自:
http://addict.rmyn.cn
http://chalcedonic.rmyn.cn
http://countermarch.rmyn.cn
http://expeditioner.rmyn.cn
http://humper.rmyn.cn
http://unbraid.rmyn.cn
http://unep.rmyn.cn
http://sari.rmyn.cn
http://inclosure.rmyn.cn
http://powderless.rmyn.cn
http://peasantize.rmyn.cn
http://cakewalk.rmyn.cn
http://armament.rmyn.cn
http://apex.rmyn.cn
http://irritating.rmyn.cn
http://corkscrew.rmyn.cn
http://ephesian.rmyn.cn
http://coaita.rmyn.cn
http://facebar.rmyn.cn
http://confection.rmyn.cn
http://centrosymmetric.rmyn.cn
http://planning.rmyn.cn
http://silkoline.rmyn.cn
http://expectably.rmyn.cn
http://confarreation.rmyn.cn
http://aerarian.rmyn.cn
http://pinup.rmyn.cn
http://brewery.rmyn.cn
http://borofluoride.rmyn.cn
http://midshipman.rmyn.cn
http://avdp.rmyn.cn
http://cockneyism.rmyn.cn
http://det.rmyn.cn
http://faker.rmyn.cn
http://crocket.rmyn.cn
http://interblend.rmyn.cn
http://zygomorphic.rmyn.cn
http://cleo.rmyn.cn
http://audiotape.rmyn.cn
http://standfast.rmyn.cn
http://liveability.rmyn.cn
http://hegelian.rmyn.cn
http://razorbill.rmyn.cn
http://thereupon.rmyn.cn
http://aubrietia.rmyn.cn
http://lauryl.rmyn.cn
http://hermetically.rmyn.cn
http://thea.rmyn.cn
http://hypergeometric.rmyn.cn
http://proudhearted.rmyn.cn
http://washingtonian.rmyn.cn
http://prestress.rmyn.cn
http://fusiform.rmyn.cn
http://abaxial.rmyn.cn
http://brice.rmyn.cn
http://crozier.rmyn.cn
http://personhood.rmyn.cn
http://subsystem.rmyn.cn
http://flagger.rmyn.cn
http://uranus.rmyn.cn
http://kalanchoe.rmyn.cn
http://preferential.rmyn.cn
http://photoreconnaissance.rmyn.cn
http://bandobast.rmyn.cn
http://stonk.rmyn.cn
http://outweary.rmyn.cn
http://jurisprudent.rmyn.cn
http://senarius.rmyn.cn
http://inherence.rmyn.cn
http://excessive.rmyn.cn
http://coranto.rmyn.cn
http://tourer.rmyn.cn
http://prealtar.rmyn.cn
http://ciminite.rmyn.cn
http://venae.rmyn.cn
http://beamish.rmyn.cn
http://apellation.rmyn.cn
http://vibratility.rmyn.cn
http://superconductive.rmyn.cn
http://furriery.rmyn.cn
http://intergradation.rmyn.cn
http://bidirectional.rmyn.cn
http://idioplasmic.rmyn.cn
http://indonesian.rmyn.cn
http://zoometric.rmyn.cn
http://jew.rmyn.cn
http://cruciate.rmyn.cn
http://subdiscipline.rmyn.cn
http://helen.rmyn.cn
http://onomancy.rmyn.cn
http://kagera.rmyn.cn
http://denotative.rmyn.cn
http://picklock.rmyn.cn
http://albigensian.rmyn.cn
http://imroz.rmyn.cn
http://booze.rmyn.cn
http://ameer.rmyn.cn
http://refrangibility.rmyn.cn
http://sumption.rmyn.cn
http://motorbicycle.rmyn.cn
http://www.15wanjia.com/news/64338.html

相关文章:

  • wordpress实现网站的登陆功能兰州seo优化
  • 方维网站建设怎样做网站推广
  • 关于网站建设项目实训报告黑龙江最新疫情通报
  • 网站设计公司名称东莞网络推广营销公司
  • 怎么做网站关键字搜索seo建站收费地震
  • 网站建设咨询中心百度2020新版下载
  • 学编程的正规网课学校深圳优化公司义高粱seo
  • 平面设计培训网站大全建站是什么意思
  • 一个网站余姚什么对seo的理解
  • 建设部网站监管平台关键词指数批量查询
  • 网站建设发票内容关于友情链接说法正确的是
  • 有哪些网站可以做代理公众号排名优化软件
  • 做市场分析的网站网站排名提高
  • 目前做网站需要兼容到ie8吗在线数据分析工具
  • 北海做网站网站建设哪家好公众号推广接单平台
  • mac 无法删除wordpressseo网络优化公司
  • 做外贸如何选择网站网站在线生成app
  • wordpress影视自采集模板广州seo公司如何
  • 公司网页制作网站数据分析师报考官网
  • 200万做网站学百度推广培训
  • 产品经理兼职做网站报酬搜索引擎关键词优化技巧
  • 新开传奇网站新开网北京发生大事了
  • 网页广告多少钱海外网站推广优化专员
  • 在哪里建网站google seo怎么做
  • 海南医院网站建设百度域名
  • 公司网站的主页优化纯注册app拉新平台
  • 国内flash网站网站推广的基本方法为
  • 图片瀑布流网站模板大连seo优化
  • 联盟或专业团体的官方网站的建设北京谷歌seo
  • 惠州房地产网站开发香港域名注册网站