Scrapy добавляет нежелательную префиксную ссылку при переходе по ссылке - PullRequest
0 голосов
/ 18 марта 2019
2019-03-17 17:21:06 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://www.google.com/www.distancesto.com/coordinates/de/jugenheim-in-rheinhessen-latitude-longitude/history/401814.html> (referer: http://www.google.com/search?q=Rheinhessen+Germany+coordinates+longitude+latitude+distancesto)
2019-03-17 17:21:06 [scrapy.core.scraper] DEBUG: Scraped from <404 http://www.google.com/www.distancesto.com/coordinates/de/jugenheim-in-rheinhessen-latitude-longitude/history/401814.html>

поэтому вместо того, чтобы следовать 'www.distancesto.com/coordinates/de/jugenheim-in-rheinhessen-latitude-longitude/history/401814.html', он добавляет 'http://www.google.com/' до и, очевидно, возвращаетв неработающей ссылке.это вне меня, и я не могу понять почему.ответа нет, я даже пытался вернуть после 22 символов (нежелательная длина префикса), и он стер часть реальной ссылки.

class Googlelocs(Spider):


name = 'googlelocs'
start_urls = []

for i in appellation_list:
    baseurl =  i.replace(',', '').replace(' ', '+')
    cleaned_href = f'http://www.google.com/search?q={baseurl}+coordinates+longitude+latitude+distancesto'
    start_urls.append(cleaned_href)



def parse(self, response):


    cleaned_href = response.xpath('//*[@id="ires"]/ol/div[1]/h3/a').get().split('https://')[1].split('&')[0]
    yield response.follow(cleaned_href, self.parse_distancesto)


def parse_distancesto(self, response):
    items = GooglelocItem()

    items['appellation'] = response.xpath('string(/html/body/div[3]/div/div[2]/div[3]/div[2]/p/strong)').get()
    items['latitude'] = response.xpath('string(/html/body/div[3]/div/div[2]/div[3]/div[3]/table/tbody/tr[1]/td)').get()
    items['longitude'] = response.xpath('string(/html/body/div[3]/div/div[2]/div[3]/div[3]/table/tbody/tr[2]/td)').get()
    items['elevation'] = response.xpath('string(/html/body/div[3]/div/div[2]/div[3]/div[3]/table/tbody/tr[10]/td)').get()

    yield items

вот паук.

1 Ответ

0 голосов
/ 19 марта 2019

Я нашел ответ.

href = response.xpath ('// * [@ id = "ires"] / ol / div [1] / h3 / a / @ href'). Get ()

это был правильный путь для получения href от Google. Также я должен был принять ссылку, замаскированную Google, не пытаясь изменить ее, чтобы иметь возможность перейти по ней.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...