【发布时间】:2018-11-11 16:39:43
【问题描述】:
我正在使用 BeautifulSoup 从多个 URL 中抓取。 URL 通过附加一个我保存在数据框 (postcode_URL) 中的变量来迭代。
代码断线:table_rows = table.find_all('tr'),抛出错误:'NoneType' object has no attribute 'find_all'
有趣的是,如果我删除迭代并在 URL 中手动输入单个邮政编码,代码可以完美运行,所以我相信它一定与迭代循环有关。
以下是我使用的代码。有什么想法吗?
scraped_data = []
for x, row in postcodes_for_urls.iterrows():
page = requests.get("http://myurl"+(row['postcode_URL']))
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find('table')
table_rows = table.find_all('tr')
for tr in table_rows:
td = tr.find_all('td')
row = [tr.text for tr in td]
scraped_data.append(row)
pd.DataFrame(scraped_data, columns=["A", "B", "C"])
【问题讨论】:
-
我会建议改用 scrapy (scrapy.org)。
标签: python dataframe web-scraping beautifulsoup