【发布时间】:2017-05-20 06:14:03
【问题描述】:
意图:
1.使用 Selenium 访问 http://blogdobg.com.br/ 的主页。
2.识别文章链接
3.将每个链接插入bs4并拉取文字
问题: 我可以打印所有链接或将单个链接移动到 bs4 用于解析和打印。我阅读每个链接的尝试以多次迭代的同一个链接结束。
我两天前才开始学习自己,所以任何指点都将不胜感激。
from selenium import webdriver
from lxml import html
import requests
import re
from bs4 import BeautifulSoup
def read (html):
html = browser.page_source
soup = BeautifulSoup(html,"html.parser")
for string in soup.article.stripped_strings:
print(repr(string))
path_to_chromedriver = '/Users/yakir/chromedriver'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://blogdobg.com.br/'
browser.get(url)
articles = browser.find_elements_by_xpath("""//*[contains(concat( " ", @class, " " ), concat( " ", "entry-title", " " ))]//a""")
#get all the links
for link in articles:
link.get_attribute("href")
#Attempt to print striped string from each link's landing page
for link in articles:
read(link.get_attribute("href"))
##method for getting one link to work all the way through (currently commented out)
#article1 = articles[1].get_attribute("href")
#browser.get(article1)
#read(article1)
【问题讨论】:
标签: python python-3.x selenium selenium-webdriver bs4