Loading

python get请求抓取网页

import requests

url = 'https://www.douban.com/search?'

# req = requests.get(url)


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.47"
}
params ={'q':'长江七号'}
# print(req.text)  #网站的源码
# print(req.url) #访问的网页
# print(req.status_code) #响应的状态码  成功返回200
# print(req.headers)  #返回响应头

# get简单请求
req = requests.get(url=url,params=params,headers=headers)
# #设置编码格式  否则会乱码   但很多時候用不上
# req.encoding = 'utf-8'
content = req.text
print(content)

 

posted @ 2023-04-12 16:24  mingBolg  阅读(65)  评论(0编辑  收藏  举报