【问题标题】:Scrape all links from a website using beautiful soup or selenium使用漂亮的汤或硒从网站上抓取所有链接
【发布时间】:2020-08-14 13:09:13
【问题描述】:

我想抓取网站拥有的所有链接,并希望将它们过滤掉,以便稍后获取它们。

问题是给定一个 URL 让我们说

URL = "https://stackoverflow.com/questions/"

我的刮刀应该刮掉并提供诸如

之类的网址
https://stackoverflow.com/questions/51284071/how-to-get-all-the-link-in-page-using-selenium-python
https://stackoverflow.com/questions/36927366/how-to-get-the-link-to-all-the-pages-of-a-website-for-data-scrapping 
https://stackoverflow.com/questions/46468032/python-selenium-automatically-load-more-pages

目前,我从 StackOverflow 借用了代码

import requests
from bs4 import BeautifulSoup

def recursiveUrl(url, link, depth):
    if depth == 10:
        return url
    else:
        # print(link['href'])
        page = requests.get(url + link['href'])
        soup = BeautifulSoup(page.text, 'html.parser')
        newlink = soup.find('a')
        if len(newlink) == 0:
            return link
        else:
            return link, recursiveUrl(url, newlink, depth + 1)
def getLinks(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')
    links = soup.find_all('a')
    for link in links:
      try:
        links.append(recursiveUrl(url, link, 0))
      except Exception as e:
        pass
    return links
links = getLinks("https://www.businesswire.com/portal/site/home/news/")
print(links)

我认为不是浏览所有页面,而是浏览网页中提供的所有超链接。

我也提到过这个

link = "https://www.businesswire.com/news"

from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider
from scrapy.http import Request

DOMAIN = link
URL = 'http://%s' % DOMAIN

class MySpider(BaseSpider):
    name = DOMAIN
    allowed_domains = [DOMAIN]
    start_urls = [
        URL
    ]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        for url in hxs.select('//a/@href').extract():
            if not ( url.startswith('http://') or url.startswith('https://') ):
                url= URL + url
            print (url)
            yield Request(url, callback=self.parse)

但这太旧了,无法正常工作。

刮痧对我来说是新的,所以我可能会陷入一些基本的基础。

让我知道如何解决这个问题。

【问题讨论】:

    标签: python-3.x selenium web-scraping scrapy


    【解决方案1】:
    import unittest
    import pytest
    from selenium import webdriver
    
    class TestHabilitado(unittest.TestCase):
      def setUp(self):
        self.driver = webdriver.Firefox()
        self.vars = {}
    
      def test_habilitado(self):
        self.driver.get("https://stackoverflow.com/questions")
    
        for link in self.driver.find_elements_by_xpath("//a[contains(@class,'question-hyperlink')]"):
            url=link.get_attribute("href")
            print(url)
    
    if __name__ == "__main__":
        unittest.main()
    

    我认为这可能是有效的。您必须安装 Selenium 依赖项并下载 firefox Selenium 驱动程序。接下来执行这个脚本。

    输出: python stackoverflow.py https://stackoverflow.com/questions/61519440/how-do-i-trigger-a-celery-task-from-django-admin https://stackoverflow.com/questions/61519439/how-to-add-rows-in-consecutive-blocks-in-excel https://stackoverflow.com/questions/61519437/not-null-constraint-failed-api-userlog-browser-info-id-when-i-want-to-add-show https://stackoverflow.com/questions/61519435/dart-parse-date-with-0000 https://stackoverflow.com/questions/61519434/is-there-a-way-to-reduce-the-white-pixels-in-a-invereted-image https://stackoverflow.com/questions/61519433/querying-datastore-using-some-of-the-indexed-properties https://stackoverflow.com/questions/61519431/model-checkpoint-doesnt-create-a-directory https://stackoverflow.com/questions/61519430/why-is-the-event-dispatched-by-window-not-captured-by-other-elements https://stackoverflow.com/questions/61519426/live-sass-complier-in-vs-code-unfortunately-stopped-working-while-coding ……

    【讨论】:

    • 我认为它正在从主页中删除网址,它是否适用于第二页上的链接?如果我使用我提到的第二个 URL 怎么办?它会在不改变任何东西的情况下工作吗?
    • 好的,你应该这样做,直到“下一页”不存在为止...
    • 我知道了,我会遍历页码,然后保存所有链接。
    【解决方案2】:

    使用requestsbs4 的一种解决方案:

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://stackoverflow.com/questions/"
    html = requests.get(url).content
    soup = BeautifulSoup(html, "html.parser")
    
    # Find all <a> in your HTML that have a not null 'href'. Keep only 'href'.
    links = [a["href"] for a in soup.find_all("a", href=True)]
    print(links)
    

    输出:

    [
        "#",
        "https://stackoverflow.com",
        "#",
        "/teams/customers",
        "https://stackoverflow.com/advertising",
        "#",
        "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f",
        "https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent",
        "https://stackoverflow.com",
    ...
    

    如果您想保留questions 链接,那么:

    print(
        [
            link
            if link.startswith("https://stackoverflow.com")
            else f"https://stackoverflow.com{link}"
            for link in links
            if "/questions/" in link
        ]
    )
    

    输出:

    [
        "https://stackoverflow.com/questions/ask",
        "https://stackoverflow.com/questions/61523359/assembly-nasm-print-ascii-table-using-a-range-determined-by-input",
        "https://stackoverflow.com/questions/tagged/assembly",
        "https://stackoverflow.com/questions/tagged/input",
        "https://stackoverflow.com/questions/tagged/range",
        "https://stackoverflow.com/questions/tagged/ascii",
        "https://stackoverflow.com/questions/tagged/nasm",
        "https://stackoverflow.com/questions/61523356/can-i-inject-an-observable-from-a-parent-component-into-a-child-component",
        "https://stackoverflow.com/questions/tagged/angular",
        "https://stackoverflow.com/questions/tagged/redux",
    ...
    ]
    
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多