我想在 https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=test 中获取下一页
但是我的代码不起作用。
请指导我。非常感谢。
scrapy shell "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=test"
response.xpath("//a[@id='pnnext']/@href")
请您参考如下方法:
这是工作代码
scrapy shell "https://www.google.com.tw/search?q=test"
response.xpath("//a[@id='pnnext']/@href")
问题在于您向 google 发出请求的方式。
无论如何请注意有关 Google 搜索的政策。
可以在 http://www.google.com/cse/docs/tos.html 找到 Google 的自定义搜索服务条款 (TOS) .
更新: 我写了一个蜘蛛来更深入地测试这个问题。
根本不是 pythonic(欢迎改进),但我对处理 google 结果的机制很感兴趣。
正如之前的评论所建议的,需要对接口(interface)的国际化进行测试。
class googleSpider(CrawlSpider):
name = "googlish"
allowed_domains = ["google.com"]
start_urls = ["http://www.google.com"]
def __init__(self):
self.driver = webdriver.Firefox()
def parse(self, response):
self.driver.get(response.url)
login_form = self.driver.find_element_by_name('q')
login_form.send_keys("scrapy\n")
time.sleep(4)
found = False
while not found:
try :
for element in self.driver.find_elements_by_xpath("//div[@class='rc']"):
print element.text + "\n"
for i in self.driver.find_elements_by_id('pnnext'):
i.click()
time.sleep(5)
except NoSuchElementException:
found = True
pass
self.driver.close()
