Python中文
首页
教程
问答
标签
搜索
登录
注册
如何用硒刮纸?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我想通过硒刮一个网站,共有10页。我的代码如下,但为什么我只能得到第一页的结果:</p> <pre><code># -*- 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)) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我猜这和你在页码作业中做的奇怪的事情有关。若要调试,请尝试在调用驱动程序。获取(网址):</p> <pre><code>print(driver.current_url) </code></pre> <p>如果它返回您期望的url,那么问题很可能出在XPATH中。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
5 回答
Jupyter运行不同版本的卸载库?
6 回答
Jupyter运行指定的键盘快捷键
1 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
5 回答
Jupyter重新加载自定义样式
9 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
4 回答
jupyter错误:无法在随机林中将决策树视为png
1 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
10 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
2 回答
Jupyter隐藏数据帧索引,但保留原始样式
10 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
10 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
10 回答
Jupy上Python的读/写访问问题
5 回答
jupy上没有模块cv
4 回答
Jupy上的排序错误
1 回答
Jupy中bqplot图形的紧凑布局
9 回答
Jupy中matplotlib plot的连续更新
3 回答
Jupy中Numpy函数的文档
4 回答
Jupy中Pandas的自动完成问题
10 回答
jupy中Qt后端的Matplotlib动画
7 回答