python爬虫工程师--手把手教会你--04发送get请求.py

本文介绍了如何在Python中使用urllib库的Request对象发送GET请求,重点讲解了通过浏览器复制粘贴、urllib.parse.quote和urllib.parse.urlencode进行中文字符URL编码的方法,以确保爬虫能正确获取数据。
摘要由CSDN通过智能技术生成

在目前网络获取数据的方式有多种方式:GET方式

大部分被传输到浏览器的html,images,js,css, … 都是通过GET方法发出请求的。它是获取数据的主要方法

Get请求的参数都是在Url中体现的,如果有中文,需要转码,这时我们可使用

方法一:直接去浏览器里面复制,粘贴过来的汉字内容自动会转码。

from urllib.request import urlopen,Request
#在浏览器中, url = https://cn.bing.com/search?pglt=41&q=python爬虫
url = 'https://cn.bing.com/search?pglt=41&q=python%E7%88%AC%E8%99%AB'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0' 
headers = { 'User-Agent' : user_agent } 

request = Request(url, headers=headers) 
response = urlopen(request) 
page = response.read().decode()[:1500]

print(page)

方法二:urllib.parse. quote() 转换一个值

from urllib.request import urlopen,Request
from urllib.parse import quote
# urllib.parse. quote() 转换一个值
arg = "python爬虫师"
url = f'https://cn.bing.com/search?pglt=41&q={quote(arg)}'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0' 
headers = { 'User-Agent' : user_agent } 

request = Request(url, headers=headers) 
response = urlopen(request) 
page = response.read().decode()[:1500]

print(page)

方法三:urllib.parse.urlencode() 转换键值对

from urllib.request import urlopen,Request
from urllib.parse import quote,urlencode
# urllib.parse.urlencode() 转换键值对
arg = {"q":"python爬虫大师"}
url = f'https://cn.bing.com/search?pglt=41&q={urlencode(arg)}'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0' 
headers = { 'User-Agent' : user_agent } 

request = Request(url, headers=headers) 
response = urlopen(request) 
page = response.read().decode()[:1500]

print(page)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要争就争第一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值