【问题标题】:How to make an effective crawler in Python2.7Python2.7如何做一个有效的爬虫
【发布时间】:2015-11-23 15:18:13
【问题描述】:

我正在抓取一些衣服网络以获取它们的价格和可用的每种产品的信息,但使用我的实际算法需要几天时间才能完成并获取每种产品的每个不同链接。例如,如果产品有 5 种颜色的 5 个链接,它会执行 5 个链接,并且我有一个包含 92k 条目的数据库,并且只有 5k 个产品,例如,像这样:

https://i.gyazo.com/410d4faa33e2fbccf8979c5399856dd3.png

https://i.gyazo.com/b6118e67205d153272df001fb5efcfc8.png

相同的产品 ID(所以相同的产品),但不同的链接。

所以我想要一些想法以及如何实施它们来改进它,例如我有产品 ID,所以如果我已经访问了一个包含该 ID 的链接,我不想再次进入它。而且我想抓取所有网络,但只访问包含产品的网络......但我不知道如何实现这两个想法:/

这是我目前的代码(太慢而且重复了很多次):

import urllib
import urlparse
from itertools import ifilterfalse
from urllib2 import URLError, HTTPError

from bs4 import BeautifulSoup

urls = {"http://www.kiabi.es/"}
visited = set()


def get_html_text(url):
    try:
        return urllib.urlopen(current_url.encode('ascii','ignore')).read()
    except (IOError,URLError, HTTPError, urllib.ContentTooShortError):
        print "Error getting " + current_url
        urls.add(current_url)


def find_internal_links_in_html_text(html_text, base_url):
    soup = BeautifulSoup(html_text, "html.parser")
    links = set()
    for tag in soup.findAll('a', href=True):
        url = urlparse.urljoin(base_url, tag['href'])
        domain = urlparse.urlparse(base_url).hostname
        if domain in url:
            links.add(url)
    return links


def is_url_already_visited(url):
    return url in visited


while urls:
  try:
    word = '#C'
    current_url = urls.pop()
    print "Parsing", current_url
    if word in current_url:


        print "Parsing", current_url
        htmltext= urllib.urlopen(current_url).read()
        soup= BeautifulSoup(htmltext)

        [get the product info and save it into a sql database]

    html_text = get_html_text(current_url)
    visited.add(current_url)
    found_urls = find_internal_links_in_html_text(html_text, current_url)
    new_urls = ifilterfalse(is_url_already_visited, found_urls)
    urls.update(new_urls)

except Exception:
    pass  

例如,在那个爬虫中,我使用单词“#C”来知道它是一个产品页面并获取它的信息,但我不知道如何区分该 url 是否有我已经拥有的产品 ID访问过,我不知道如何避免不相关的 url,这就是为什么程序太慢并且得到很多相等的链接

感谢您的帮助,任何可以改进的地方都会很棒^^

【问题讨论】:

  • 您是否考虑过改用scrapy
  • 好吧,我对scrapy的想法是0:/不知道将其翻译成scrapy并添加我正在寻找的规则

标签: python web-scraping beautifulsoup web-crawler


【解决方案1】:

我建议使用Scrapy。我的回答是基于它的强大功能。

引用你的疑问:


我不知道如何避免不相关的网址

回答:为避免访问不相关的 URL,您应该根据您的用例使用具有特定逻辑的爬虫。也就是说,您可以使用 CrawlSpider 并定义自己的 Crawling rules,其中每个规则都定义了抓取网站的特定行为,这样您就不会访问不相关的 URL。

这里是一个例子:http://doc.scrapy.org/en/latest/topics/spiders.html#crawlspider-example


...但我不知道如何区分该网址是否有产品 ID 我已经去过...

答案: 默认情况下,Scrapy 使用 RFPDupeFilter 作为其DUPEFILTER_CLASS,用于检测和过滤重复请求。 RFPDupeFilter 使用scrapy.utils.request.request_fingerprint 函数根据请求指纹进行过滤。

这是重复请求的 Scrapy 日志输出示例:

2015-11-23 14:26:48 [scrapy] DEBUG: Filtered duplicate request: <GET http://doc.scrapy.org/en/latest/topics/request-response.html> - no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates)

如果您还没有使用过,这里有一个 Scrapy 教程:http://doc.scrapy.org/en/latest/intro/tutorial.html

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多