【发布时间】:2018-08-13 16:12:19
【问题描述】:
我正在运行一个抓取工具来检索产品名称、货号、尺寸和价格,但是当我运行脚本时它没有给我输出或错误消息。我正在为此使用 Jupyter Notebook,但不确定这是否是问题所在。我也不确定是否因为我将其输入到 CSV 文件中,如果这也给它带来了问题。任何帮助将不胜感激。
这是我正在运行的代码。
from selenium import webdriver
import csv, os
from bs4 import BeautifulSoup
os.chdir(r'C:\Users\kevin.cragin\AppData\Local\pip\Cache\wheels\09\14\7d\1dcfcf0fa23dbb52fc459e5ce620000e7dca7aebd9300228fe')
driver = webdriver.Chrome()
driver.get('https://www.biolegend.com/en-us/advanced-search?GroupID=&PageNum=1')
html = driver.page_source
containers = html.find_all('li', {'class': 'row list'})
with open("BioLegend_Crawl.csv", "w") as f:
f.write("Product_name, CatNo, Size, Price\n")
for container in containers:
product_name = container.find('a',{'itemprop':'name'}).text
info = container.find_all('div',{'class':'col-xs-2 noPadding'})
catNo = info[0].text.strip()
size = info[1].text.strip()
price = info[2].text.strip()
print('Product_name: '+ product_name)
print('CatNo: ' + catNo)
print('Size: ' + size)
print('Price: ' + price + '\n')
f.write(','.join([product_name,catNo,size,price]))
【问题讨论】:
-
你有没有检查
containers的大小看它是否为空? -
另外,您没有在脚本中的任何地方使用
headers字符串。你的意思是f.write(headers)而不是f.write('header')? -
@rahlf23 我分别测试了每个容器,看看它们是否会拉动,它们会拉动,但当我将它们全部放在同一个脚本中时,它们不会拉动。此外,我不确定是否由于此页面的大小是否也会给它带来问题。
-
我将在下面发布您的脚本的简化版本供您测试。今天晚些时候,我可以在没有防火墙的家用笔记本电脑上进行测试,以确保它可以正常工作。
-
如果您有兴趣提取的内容不是动态加载的,那么如果内容的大小(信息量)实际上会减慢您的速度,我会感到非常惊讶......
标签: python selenium selenium-webdriver web-scraping beautifulsoup