写个python代码,内容是爬取一次百度

时间2023-07-10 16:49:33 提问来自 加州洛杉矶 的网友 分类ChatGPT提问 浏览173 热度0
# -*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com/' #百度首页的URL地址 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'} #伪装浏览器请求头 response = requests.get(url, headers=headers) #发送请求,获取网页内容 html = response.content #获取网页内容,得到的是bytes格式数据,一般用于下载图片、视频、音乐之类的 html_doc = str(html, 'utf-8') #将bytes格式数据转化为字符串str格式 #使用BeautifulSoup进行解析,因为原始文件是html文件所以使用“html5lib”作为parser soup = BeautifulSoup(html_doc, 'html5lib') hot_list=[] for hot in soup("div",class_="hotnews"): for a in hot("a"): hot_list+=[[a["href"],a["title"]]] print(hot_list)

AI智能问答网免责声明:
以上内容除特别注明外均来源于网友提问、ChatGPT回答,权益归原著者所有,本站仅作效果演示和欣赏之用;
若以上展示有冒犯或侵害到您,敬请联系我们进行删除处理,谢谢!

上一篇 写个python代码,内容是定时每5秒执 下一篇 搜索框的用法