Удалите все ссылки с веб-сайта, используя красивый суп или селен - PullRequest
1 голос
/ 30 апреля 2020

Я хочу удалить все ссылки, которые есть на веб-сайте, и хочу отфильтровать их, чтобы я мог их потом увидеть.

Проблема в том, что URL-адрес позволяет сказать

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

мой скребок должен удалить и предоставить URL-адреса, такие как

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)

Но это слишком старо и не работает.

Утилизация для меня новость, поэтому я могу застрять в некоторых основах c фундамент.

Дайте мне знать, как решить эту проблему.

Ответы [ 2 ]

2 голосов
/ 30 апреля 2020
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. Затем выполните этот скрипт.

OUTPUT:
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
....
0 голосов
/ 30 апреля 2020

Одно решение с использованием requests и bs4:

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",
...
]

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...