导入库:
import requests
from bs4 import BeautifulSoup
获取html文件:
def getHtml(url):
while(True):
try:
res = requests.get(url, timeout=2, headers={'User-Agent': 'Baiduspider'})
break
except:
time.sleep(1)
encode = res.encoding
s = res.content
s.decode(encode)
return s
生成实例:
html = getHtml(url)
bs = BeautifulSoup(html,"html.parser")
返回所有class为listto的a标签:
bs.find_all('a',class_='listto')
获取所有span标签:
day.find_all('span')
获取某个标签的内容:
res[0].text
获取某个标签的属性:
res.get('href')
- 例如class
最后
以上就是野性斑马最近收集整理的关于python爬虫 BeautifulSoup的全部内容,更多相关python爬虫内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复