【发布时间】: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