使用 dryscrape 抓取 react.js 网页
Scraping a react.js webpage with dryscrape
我无法抓取使用 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!")
我认为你的 xpath 有问题,但除此之外,你的会话本身配置不正确。
这一行
session = dryscrape.Session(base_url = root_url)
将 URL 的基数设置为 root_url
,因此当您执行 session.visit('http://www.jobs.ch/en/vacancies/')
时,您实际上是在访问 root_url 和 URL 在 session.visit.
中提供
如果您 print session.url()
您将能够看到您实际访问的 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。
我无法抓取使用 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!")
我认为你的 xpath 有问题,但除此之外,你的会话本身配置不正确。
这一行
session = dryscrape.Session(base_url = root_url)
将 URL 的基数设置为 root_url
,因此当您执行 session.visit('http://www.jobs.ch/en/vacancies/')
时,您实际上是在访问 root_url 和 URL 在 session.visit.
如果您 print session.url()
您将能够看到您实际访问的 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。