python request爬虫的快速使用、简单、快速上手

本文介绍了如何使用Python的requests库向网站发送GET和POST请求,包括设置URL、请求头、cookies、代理以及使用正则表达式或XPath解析响应内容。
摘要由CSDN通过智能技术生成

Python requests 是一个常用的 HTTP 请求库,可以方便地向网站发送 HTTP 请求,并获取响应结果。

一、导入requests包

二、设置url(网址,列:http//www/baidu.com)

三、requests发送请求get/post(设置配置参数)【也可只设置url,但可能会出错】列:

import requests
# 设置url
url_path = f"http://www.baidu.com"

# 设置请求头
User_Agent = {
    'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
    'Connection': 'close'
}

coo = " balh_server_custom_tw=https://bili.tuturu.top;" \
    " balh_server_custom_hk=https://bili-proxy.98e.org;" \
    " balh_server_custom=https://bili.tuturu.top;"
coos = coo.split(";")

# 以字典的方式储存
cookies_dict = {cookies.split("=")[0]: cookies.split("=")[-1] for cookies in coos}

# cookies的设置
proxies = {
    "http": "http://111.40.62.176:9091",
    "https": "http://111.40.62.176:9091"
}

# url == 请求网址    headers == 请求头   cookies == cookies参数请求    proxies == ip代理      verify=False == 忽略CA证书
# text(返回响应的内容,unicode 类型数据---字符串)  content(返回响应的内容,以字节为单位---二进制)

reopen = requests.get(url=url_path, headers=User_Agent, cookies=cookies_dict, proxies=proxies, verify=False).text

print(reopen)

四、可使用re正则表达式xpath获取标签内容,列:

'''由上得到的reopen'''

import re

xe = '<li>.*?<img src="(.*?)" alt.*?</li>'

img_src = re.findall(xe, reopen, re.S)

print(img_sre)


# 或

from lxml import etree

er = etree.HTML(reopen)

data = er.xpath("/html/body/div[3]/div/div/ul[1]/li[2]/span[2]/a/@href")

print(date)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值