努力尝试在python

时间:2018-09-12 15:14:29

标签: python scrapy

我正在尝试在黄页中刮除每个企业的href。第二天我对使用scrapy非常陌生。我正在使用请求来获取实际的URL以使用Spider搜索。我的代码在做什么错?我想最终让每个公司都抓紧时间,抓取其地址和其他信息。

# -*- coding: utf-8 -*-
import scrapy
import requests

search = "Plumbers"
location = "Hammond, LA"
url = "https://www.yellowpages.com/search"
q = {'search_terms': search, 'geo_location_terms': location}
page = requests.get(url, params=q)
page = page.url

class YellowpagesSpider(scrapy.Spider):
    name = 'quotes'
    allowed_domains = ['yellowpages.com']
    start_urls = [page]

    def parse(self, response):
        self.log("I just visited: " + response.url)
        items = response.css('span.text::text')
        for items in items:
            print(items)

1 个答案:

答案 0 :(得分:2)

要获取名称,请使用:

response.css('a[class=business-name]::text')

要获取href使用:

response.css('a[class=business-name]::attr(href)')

在最终通话中,它看起来像:

    for bas in response.css('a[class=business-name]'):
        item = { 'name' : bas.css('a[class=business-name]::text').extract_first(),
                  'url' : bas.css('a[class=business-name]::attr(href)').extract_first() }
        yield item

结果:

2018-09-13 04:12:49 [quotes] DEBUG: I just visited: https://www.yellowpages.com/search?search_terms=Plumbers&geo_location_terms=Hammond%2C+LA
2018-09-13 04:12:49 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.yellowpages.com/search?search_terms=Plumbers&geo_location_terms=Hammond%2C+LA>
{'name': 'Roto-Rooter Plumbing & Water Cleanup', 'url': '/new-orleans-la/mip/roto-rooter-plumbing-water-cleanup-21804163?lid=149760174'}
2018-09-13 04:12:49 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.yellowpages.com/search?search_terms=Plumbers&geo_location_terms=Hammond%2C+LA>
{'name': "AJ's Plumbing And Heating Inc", 'url': '/new-orleans-la/mip/ajs-plumbing-and-heating-inc-16078566?lid=1001789407686'}
...