【发布时间】:2019-06-14 02:39:03
【问题描述】:
我想从这个特定的网站收集文章。我之前只是在使用 Beautifulsoup,但它没有抓取链接。所以我尝试使用硒。现在我试着写这段代码。这给出了输出“无”。我以前从未使用过硒,所以我对此不太了解。我应该在此代码中进行哪些更改以使其正常工作并提供所需的结果?
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
base = 'https://metro.co.uk'
url = 'https://metro.co.uk/search/#gsc.tab=0&gsc.q=cybersecurity&gsc.sort=date&gsc.page=7'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
wait = WebDriverWait(browser, 10)
browser.get(url)
link = browser.find_elements_by_class_name('gs-title')
for links in link:
links.get_attribute('href')
soup = BeautifulSoup(browser.page_source, 'lxml')
date = soup.find('span', {'class': 'post-date'})
title = soup.find('h1', {'class':'headline'})
content = soup.find('div',{'class':'article-body'})
print(date)
print(title)
print(content)
time.sleep(3)
browser.close()
我想收集此页面上所有文章的日期、标题和内容,以及其他页面,如第 7 到 18 页。
谢谢。
【问题讨论】:
标签: selenium-webdriver web-scraping beautifulsoup