【发布时间】:2021-08-01 15:00:02
【问题描述】:
我正在尝试从this website 中刮取一些数据。 我将不胜感激任何帮助。
每页有 30 个条目,我目前正在尝试从每页上的每个链接中抓取信息。这是我的代码:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
print(driver.title)
driver.get("https://www.businesslist.com.ng/category/farming")
time.sleep(1)
select = driver.find_element_by_id("listings")
page_entries = [i.find_element_by_tag_name("a").get_attribute("href")
for i in select.find_elements_by_tag_name("h4")]
columns = {"ESTABLISHMENT YEAR":[], "EMPLOYEES":[], "COMPANY MANAGER":[],
"VAT REGISTRATION":[], "REGISTRATION CODE":[]}
for i in page_entries:
print(i)
driver.get(i)
listify_subentries = [i.text.strip().replace("\n","") for i in
driver.find_elements_by_class_name("info")][:11]
到目前为止一切正常。问题可能在下面的部分。
for i in listify_subentries:
for q in columns.keys():
if q in i:
item = i.replace(q,"")
print(item)
columns[q].append(item)
else:
columns[q].append("None given")
print("None given")
Here's a picture of the layout for one entry. Sorry I can't yet embed images.
我正在尝试从每个企业的页面上抓取“工作时间”框下的一些信息(即成立年份、公司经理等)。您可以在columns 变量下找到确切的信息。
由于并非所有页面的“工作时间”框(here is one with more details underneath it)下的信息量都相同,因此我尝试使用字典+文本操作来查找可用的子条目并获取其右侧的相关信息.即获取公司经理姓名、成立年份等;如果一个页面没有这个,那么它只会在相关的子条目下被标记为“None given”。
我们的想法是整理所有这些信息,然后将其导出到数据框。当页面缺少特定子条目时输入“None given”可以让我保持数据结构的完整性,从而确保条目对齐。
但是,当我运行代码时,我收到的输出完全关闭。
Here is the outer view of the columns dictionary once the code has run.
And if I click on the 'COMPANY MANAGER' section, you can see that there are multiple instances of it saying "None given" before it gives the name of company manager on the page. 如果运行代码并向下滚动,您将看到每个其他子条目都会重复此操作。我不确定出了什么问题,但似乎列表的大小已经膨胀了 10 倍,到处都是额外的“None given”。每个列表的大小应该是 30,但现在是 330。
我将不胜感激任何帮助。谢谢。
【问题讨论】:
标签: python selenium web-scraping data-structures