【问题标题】:Web Scraping tables and data with Python Beautifulsoup使用 Python Beautifulsoup 抓取表格和数据
【发布时间】:2019-12-18 10:45:59
【问题描述】:

我已经使用 Python-Beautifulsoup 将这个表中的数据从这个 website 的所有页面中抓取到一个字典中,如下面的代码所示。

但是,我也尝试将每个拥有自己独立 page 的公司也搜索到该字典中。

import requests 
from bs4 import BeautifulSoup
from pprint import pprint 

company_data = []

for i in range(1, 3):
    page = requests.get(f'https://web.archive.org/web/20121007172955/http://www.nga.gov/collection/anZ1.htm{i}?')
    soup = BeautifulSoup(page.text, "lxml")

    row_info = soup.select('div.accordion_heading.panel-group.s_list_table')

    for row_info in row_info:
        comapny_info = {}
        comapny_info['Name'] = row_info.select_one('div.col_1 a').text.strip()

pprint(company_data)

【问题讨论】:

  • 所以你需要爬取你提到的URL并相应地更新字典
  • 这已经完成,从 company_info['Profile'] 等可以看出。但是我不确定如何提取 ACOP 报告提交表 - (仅需要链接)和组织对可持续发展的承诺(问题和答案)进入字典
  • soup.findAll("tr") 应该给你所有的tr
  • 在这种情况下只返回一个空列表

标签: python html python-3.x web-scraping beautifulsoup


【解决方案1】:

我刚刚完成了只为 2M 公司 我相信这会有所帮助。

import requests
from bs4 import BeautifulSoup
res=requests.get("https://web.archive.org/web/20121007172955/http://www.nga.gov/collection/anZ1.htm").text
soup=BeautifulSoup(res,'html.parser')
comapny_info={}
comapny_info['Profile'] = soup.select('div.text-desc-members')
if len(soup.select('div.text-desc-members'))==0:
  comapny_info['Profile']  = soup.select('div.list-sub')[0].text.strip()

comapny_info['ACOP']=[item['href'] for item in soup.select(".table.table-striped a.files")]
comapny_info['QuestionAnswer']=["Question:" + q.text.strip() + " Answer:" +a.text.strip() for q ,a in zip(soup.select("div.list-reports .m_question"),soup.select("div.list-reports .m_answer")) ]

print(comapny_info)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2021-06-23
    • 2011-03-11
    相关资源
    最近更新 更多