[英]BeautifulSoup website scraping - html parsing
我正在尝试使用beautifulsoup4从网站上抓取数据,并且仅检索要放入Excel文档中的html标记之间的信息,目前我只能从页面中获取整个html数据。
import sys
import urllib3
import xlsxwriter
import lxml
page = requests.get('genericurlhere.com')
soup = BeautifulSoup(page.text, 'html.parser')
f = csv.writer(open('web_scrape.csv', 'w'))
f.writerow(['Item', 'Description'])
heading = soup.find_all("h4", class_="list-group-item-heading")
print(heading)
print('-------------------')
desc = soup.find_all("p", class_='list-group-item-text')
print(desc)
尝试使用text
:
desc = soup.find_all("p", class_='list-group-item-text')
desc = [e.text for e in desc] # only text within tags from the html elements.
print(desc)
请注意,您还可以使用[]
获取html标签的属性,例如: each['id']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.