BeautifulSoup简单应用(python爬虫)

BeautifulSoup简单应用(python爬虫),第1张

  1. 打开cmd安装
pip install beautifulsoup4

根据属性定位

server=soup.find(attrs={'id':'hao123-govsite'})

定位server下的a标签

a=server.a

获取a标签的中的href和文字

text=a.text
text=a['href']

根据属性定位div下的所有a标签

a=soup.find_all(attrs={'class':'g-gc'})
import requests
from bs4 import BeautifulSoup
url=''
header={
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39"
}
r=requests.get(url,headers=header)
demo=r.text
soup=BeautifulSoup(demo,"html.parser")
# print(soup.prettify()) #代码格式话
server=soup.find(attrs={'id':'hao123-govsite'})
a=server.a
href=a['href']
text=a.text
a=soup.find_all(attrs={'class':'g-gc'})
for i in a:
    href=i['href']
    text=i.text

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/915643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存