【问题标题】:Python 3 How to Scrape/Crawl on Specific Domains?Python 3 如何在特定域上抓取/爬行?
【发布时间】:2020-05-24 18:47:59
【问题描述】:

我希望抓取所有 url/文本内容并在特定域上抓取。

我见过一种抓取网址的方法 (retrieve links from web page using python and BeautifulSoup)

我也尝试了以下停留在特定域的代码,但它似乎并不完全有效。

domains = ["newyorktimes.com", etc]
p = urlparse(url)
print(p, p.hostname)
if p.hostname in domains:
    pass
else:
    return []

#do something with p

我的主要问题是确保爬虫停留在指定的域中,但是当 url 可能具有不同的路径/片段时,我不确定如何做到这一点。我知道如何从给定网站上抓取网址。我愿意使用 BeautifulSoup、lxml、scrapy 等

这个问题可能有点太宽泛了,但我尝试过在特定域中搜索有关爬取的信息,但找不到太相关的东西:/

任何帮助/资源将不胜感激!

谢谢

【问题讨论】:

  • 有图书馆可以做到这一点。你可能想看看 Scapy。

标签: python web-scraping web-crawler python-3.7


【解决方案1】:

试试这个。

from simplified_scrapy.spider import Spider, SimplifiedDoc
class MySpider(Spider):
  name = 'newyorktimes.com'
  allowed_domains = ['newyorktimes.com','nytimes.com']
  # concurrencyPer1s=1
  start_urls = 'https://www.newyorktimes.com'
  refresh_urls = True # For debug. If efresh_urls = True, start_urls will be crawled again.

  def extract(self, url, html, models, modelNames):
    doc = SimplifiedDoc(html)
    lstA = doc.listA(url=url['url'])
    return {"Urls": lstA, "Data": None} # Return data to framework

from simplified_scrapy.simplified_main import SimplifiedMain
SimplifiedMain.startThread(MySpider()) # Start crawling

这里有更多示例:https://github.com/yiyedata/simplified-scrapy-demo/tree/master/spider_examples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2016-03-21
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多