【发布时间】:2015-05-21 20:06:57
【问题描述】:
我编写此代码是为了登录我的 FB 帐户并使用 Selenuim 和 BeautifulSoup 获取页面上的所有群组链接,但 BeautifulSoup 的使用无法正常工作。
我想知道如何在同一代码中使用 Selenuim 和 BeautifulSoup。
我不想使用 Facebook API;我想使用 Selenium 和 BeautifulSoup。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer
usr = raw_input('--> ')
pwd = raw_input('--> ')
poo = raw_input('--> ')
driver = webdriver.Firefox()
# or you can use Chrome(executable_path="/usr/bin/chromedriver")
driver.get("https://www.facebook.com/groups/?category=membership")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
scheight = .1
while scheight < 9.9:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight)
scheight += .01
soup = BeautifulSoup(html)
http = httplib2.Http()
status, response = ('https://www.facebook.com/groups/?category=membership')
count = 0
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
count = count + 1
print 'Count: ', count
for tag in BeautifulSoup(('a')):
if link.has_key('href'):
if '/groups/' in link['href']:
print link['href']
elem = driver.find_element_by_css_selector(".input.textInput")
elem.send_keys(poo)
elem = driver.find_element_by_css_selector(".selected")
elem.send_keys(Keys.RETURN)
elem.click()
time.sleep(5)
【问题讨论】:
-
你需要澄清一下。 “漂亮的汤不能正常工作”是什么意思?会发生什么,这与期望的行为有何不同?
-
结果 Traceback(最近一次调用最后一次):文件“tk.py”,第 28 行,在
soup = BeautifulSoup(html) NameError: name 'html' is not defined跨度>
标签: python selenium-webdriver beautifulsoup desktop-application