【问题标题】:How to get links using selenium and scrape using beautifulsoup?如何使用 selenium 获取链接并使用 beautifulsoup 进行抓取?
【发布时间】: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


    【解决方案1】:

    我没有使用 Selenium 来获取锚点,而是尝试先在 Selenium 的帮助下提取页面源,然后在其上使用 Beautiful Soup。

    所以,换个角度来看:

    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) #Not actually required
    browser.get(url)
    soup = BeautifulSoup(browser.page_source, 'html.parser') #Get the Page Source
    anchors = soup.find_all("a", class_ = "gs-title") #Now find the anchors
    
    for anchor in anchors:
        browser.get(anchor['href']) #Connect to the News Link, and extract it's Page Source
        sub_soup = BeautifulSoup(browser.page_source, 'html.parser')
        date = sub_soup.find('span', {'class': 'post-date'})
        title = sub_soup.find('h1', {'class':'post-title'}) #Note that the class attribute for the heading is 'post-title' and not 'headline'
        content = sub_soup.find('div',{'class':'article-body'})
        print([date.string, title.string, content.string])
    
        #time.sleep(3) #Even this I don't believe is required
    browser.close()
    

    通过这个修改,相信你可以得到你需要的内容。

    【讨论】:

    • 谢谢,它正在工作。但是为什么每篇文章要打印或给两次呢?
    • 如果您看到url 的页面源代码,那么您会发现在每个结果区域中有两个位置放置了带有class="gs-title"a 标记。它们本质上都是div,但它们的类别不同。一个有class = "gsc-thumbnail-inside",另一个有class = "gs-title gsc-table-cell-thumbnail gsc-thumbnail-left"。我相信这可以通过在每个循环开始时检查当前锚值是否与之前的锚值相似来轻松解决。
    【解决方案2】:

    您可以使用与页面使用相同的 API。更改参数以获取所有页面的结果

    import requests
    import json
    import re
    
    r = requests.get('https://cse.google.com/cse/element/v1?rsz=filtered_cse&num=10&hl=en&source=gcsc&gss=.uk&start=60&cselibv=5d7bf4891789cfae&cx=012545676297898659090:wk87ya_pczq&q=cybersecurity&safe=off&cse_tok=AKaTTZjKIBzl-5fANH8dQ8f78cv2:1560500563340&filter=0&sort=date&exp=csqr,4229469&callback=google.search.cse.api3732')
    p = re.compile(r'api3732\((.*)\);', re.DOTALL)
    data = json.loads(p.findall(r.text)[0])
    links = [item['clicktrackUrl'] for item in data['results']]
    print(links)
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 2018-07-29
      • 2020-01-23
      • 2014-12-07
      • 1970-01-01
      相关资源
      最近更新 更多