python - 如何在Python2.7中制作一个有效的爬虫

标签 python web-scraping beautifulsoup web-crawler

我正在爬行一些衣服网以获取​​它们的价格以及每种可用产品的信息,但根据我的实际算法,需要几天的时间才能完成并获取每种产品的每个不同链接。例如,如果产品有 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”来知道它是一个产品页面并获取它的信息,但我不知道如何区分该网址是否具有我已经存在的产品 ID访问过,而且不知道如何避免不相关的url,这就是程序太慢并且得到很多平等链接的原因

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

最佳答案

我建议使用Scrapy 。我的回答是基于其出色的功能。

引用你的疑问:

<小时/>

I don't know how to avoid irrelevant urls

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

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

<小时/>

...but I don't know how to distinguish if that url has a product ID that I already visited...

答案:默认情况下,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

关于python - 如何在Python2.7中制作一个有效的爬虫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33874483/

相关文章:

python - 尝试在 python 中使用正则表达式

python - BeautifulSoup 和转换 HTML 实体的奇怪行为

Windows 中的 Python Popen 在同一件事上既成功又失败

python - Tensorflow:保存/恢复变量加载不正确

python - 将 Python 网络抓取的数据分离到不同的列中 (Excel)

python - 使用 BeautifulSoup,如何仅从特定选择器中获取文本而没有子项中的文本?

Python-libvirt

python - 有没有更好的方法来构建这样的列表?

python - 使用网页抓取来检查商品是否有库存

python - BeautifulSoup 只抓取最后的结果