我想通过硒刮一个网站,共有10页。我的代码如下,但为什么我只能得到第一页的结果:
# -*- coding: utf-8 -*-
from selenium import webdriver
from scrapy.selector import Selector
MAX_PAGE_NUM = 10
MAX_PAGE_DIG = 3
driver = webdriver.Chrome('C:\Users\zhang\Downloads\chromedriver_win32\chromedriver.exe')
with open('results.csv', 'w') as f:
f.write("Buyer, Price \n")
for i in range(1, MAX_PAGE_NUM + 1):
page_num = (MAX_PAGE_DIG - len(str(i))) * "0" + str(i)
url = "https://www.oilandgasnewsworldwide.com/Directory1/DREQ/Drilling_Equipment_Suppliers_?page=" + page_num
driver.get(url)
names = sel.xpath('//*[@class="fontsubsection nomarginpadding lmargin opensans"]/text()').extract()
Countries = sel.xpath('//td[text()="Country:"]/following-sibling::td/text()').extract()
websites = sel.xpath('//td[text()="Website:"]/following-sibling::td/a/@href').extract()
driver.close()
print(len(names), len(Countries), len(websites))
我猜这和你在页码作业中做的奇怪的事情有关。若要调试,请尝试在调用驱动程序。获取(网址):
如果它返回您期望的url,那么问题很可能出在XPATH中。你知道吗
在这里,我首先用
find_elements_by_xpath
获得每个页面的名称、国家和网站,并将它们存储到一个列表中。将从列表中的每个元素中提取文本,并将值添加到新列表中。你知道吗我希望这对你有用
选项:获取
<div class = border fontcontentdet>
上包含的节中的所有数据。你知道吗从selenium导入webdriver
相关问题 更多 >
编程相关推荐