【问题标题】:Scraping a react.js webpage with dryscrape使用 dryscrape 抓取 react.js 网页
【发布时间】:2017-03-10 09:54:45
【问题描述】:

我无法抓取使用 react.js 编程的主页 http://www.jobs.ch。 我想在搜索框中输入术语Business 并执行搜索。 Dryscrape 为另一个不是 react.js 页面的示例工作。

如何在此搜索字段中输入术语 Business

我的脚本执行时的错误信息:

ubuntu@ubuntu:~/scripts$ python jobs.py
Traceback (most recent call last):
  File "jobs.py", line 30, in <module>
    name.set("Business")
AttributeError: 'NoneType' object has no attribute 'set'

这是我的脚本:

#We will write a Python script to visit a webpage. Fill in the form and   submit the form.
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import dryscrape

# make sure you have xvfb installed
dryscrape.start_xvfb()

root_url = 'http://www.jobs.ch/en/vacancies/'

if __name__ == '__main__':
# set up a web scraping session
session = dryscrape.Session(base_url = root_url)

# we don't need images
session.set_attribute('auto_load_images', False)

session.set_header('User-agent', 'Google Chrome')

# visit exact webpage which is the form in this example
session.visit('http://www.jobs.ch/en/vacancies/')

# fill in the form by taking ID of field from webdev tool
#name = session.at_xpath('//*[@data-reactid="107]')
name = session.at_xpath('//*[@data-reactid="107"]//*[@class="search-input col-sm-4 col-md-5"]')

name.set("Business")

# submit form
name.form().submit()

# save a screenshot of the web page
session.render("jobs.png")
print("Session rendered successfully!")

【问题讨论】:

    标签: python web-scraping dryscrape


    【解决方案1】:

    我认为您的 xpath 有问题,但除此之外,您的会话本身配置不正确。

    这一行

    session = dryscrape.Session(base_url = root_url)
    

    将 URL 的基础设置为您的 root_url,因此当您执行 session.visit('http://www.jobs.ch/en/vacancies/') 时,您实际上是在访问您的 root_url 和 session.visit 中提供的 URL 的串联。

    如果您print session.url(),您将能够看到您实际访问的网址是http://www.jobs.ch/en/vacancies/http://www.jobs.ch/en/vacancies/

    我从 Chrome -> Inspect -> Right Click -> Copy XPath 得到的页面的 xpath 是//*[@id="react-root"]/div/div[1]/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/div/div[1]/div/input

    请确认您使用的是正确的 xpath。

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2020-06-18
      • 2020-03-27
      • 2019-04-14
      • 2013-04-29
      相关资源
      最近更新 更多