从 Facebook 抓取组标题
Scraping group title from Facebook
我正在尝试从此页面获取一些信息:
https://www.facebook.com/groups/574916095895510/?fref=ts
当您点击"See all"时,会显示成员列表。我正在尝试获取以下文本:"Members of UCLA class of 2018 Official Group".
但是,当我尝试从 XPath 打印文本时出现以下错误:
Traceback (most recent call last):
File "scraper.py", line 35, in <module>
print title.text()
AttributeError: 'NoneType' object has no attribute 'text'
当我将 session 转储为图像时,标题可见,因此我知道文本可用于抓取。
这是我的完整代码:
import time
import dryscrape
import json
import ast
username = 'USERNAME'
password = 'PASSWORD'
# set up a web scraping session
sess = dryscrape.Session(base_url = 'https://www.facebook.com/')
# visit homepage and log in
print "Logging in..."
sess.visit('/login.php?next=https%3A%2F%2Fwww.facebook.com%2Fgroups%2F574916095895510%2F%3Ffref%3Dts')
# Set username and password
username_field = sess.at_css('#email')
password_field = sess.at_css('#pass')
username_field.set(username)
password_field.set(password)
# Submit the form
username_field.form().submit()
# Wait
time.sleep(3)
print "Viewing all members..."
see_all_button = sess.at_xpath('//*[@id="pagelet_group_profile_members"]/div/div/div/div[1]/div/a')
see_all_button.click()
time.sleep(3)
title = sess.at_xpath('//*[@id="u_z_0"]/div/div[1]/h3')
print title.text()
sess.render('fb.png')
您不应该从 Facebook 抓取数据。
Facebook 通过 API.
提供您所有的数据需求
您可以在此处的文档中找到任何组信息。这比抓取更干净,如果你想在将来使用它,将会被维护。 :)
https://developers.facebook.com/docs/graph-api/reference/v2.3/group
我已经做了很多抓取,这似乎不是抓取的好用例 - 使用 API!
我正在尝试从此页面获取一些信息: https://www.facebook.com/groups/574916095895510/?fref=ts
当您点击"See all"时,会显示成员列表。我正在尝试获取以下文本:"Members of UCLA class of 2018 Official Group".
但是,当我尝试从 XPath 打印文本时出现以下错误:
Traceback (most recent call last):
File "scraper.py", line 35, in <module>
print title.text()
AttributeError: 'NoneType' object has no attribute 'text'
当我将 session 转储为图像时,标题可见,因此我知道文本可用于抓取。
这是我的完整代码:
import time
import dryscrape
import json
import ast
username = 'USERNAME'
password = 'PASSWORD'
# set up a web scraping session
sess = dryscrape.Session(base_url = 'https://www.facebook.com/')
# visit homepage and log in
print "Logging in..."
sess.visit('/login.php?next=https%3A%2F%2Fwww.facebook.com%2Fgroups%2F574916095895510%2F%3Ffref%3Dts')
# Set username and password
username_field = sess.at_css('#email')
password_field = sess.at_css('#pass')
username_field.set(username)
password_field.set(password)
# Submit the form
username_field.form().submit()
# Wait
time.sleep(3)
print "Viewing all members..."
see_all_button = sess.at_xpath('//*[@id="pagelet_group_profile_members"]/div/div/div/div[1]/div/a')
see_all_button.click()
time.sleep(3)
title = sess.at_xpath('//*[@id="u_z_0"]/div/div[1]/h3')
print title.text()
sess.render('fb.png')
您不应该从 Facebook 抓取数据。 Facebook 通过 API.
提供您所有的数据需求您可以在此处的文档中找到任何组信息。这比抓取更干净,如果你想在将来使用它,将会被维护。 :)
https://developers.facebook.com/docs/graph-api/reference/v2.3/group
我已经做了很多抓取,这似乎不是抓取的好用例 - 使用 API!