【问题标题】:BeautifulSoup website scraping - html parsingBeautifulSoup 网站抓取 - html 解析
【发布时间】:2018-10-20 22:34:00
【问题描述】:

我正在尝试使用 beautifulsoup4 从网站上抓取数据并仅检索 html 标签之间的信息以放入 excel 文档中,目前我只能从页面中获取整个 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)

【问题讨论】:

    标签: python beautifulsoup html-parsing


    【解决方案1】:

    尝试使用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']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-15
      • 2020-03-13
      • 1970-01-01
      • 2017-08-21
      • 2016-02-22
      • 2020-05-30
      • 2020-06-27
      相关资源
      最近更新 更多