這是我用來從 tripadvisor 中抓取電子郵件地址和餐館名稱的蜘蛛
import scrapy
class RestaurantSpider(scrapy.Spider):
name = 'tripadvisorbot'
start_urls = [
'https://www.tripadvisor.com/Restaurants-g188633-The_Hague_South_Holland_Province.html#EATERY_OVERVIEW_BOX'
]
def parse(self, response):
for listing in response.xpath('//div[contains(@class,"__cellContainer--")]'):
link = listing.xpath('.//a[contains(@class,"__restaurantName--")]/@href').get()
text = listing.xpath('.//a[contains(@class,"__restaurantName--")]/text()').get()
complete_url = response.urljoin(link)
yield scrapy.Request(
url=complete_url,
callback=self.parse_listing,
meta={'link': complete_url,'text': text}
)
next_url = response.xpath('//*[contains(@class,"pagination")]/*[contains(@class,"next")]/@href').get()
if next_url:
yield scrapy.Request(response.urljoin(next_url), callback=self.parse)
def parse_listing(self, response):
link = response.meta['link']
text = response.meta['text']
email = response.xpath('//a[contains(@href, "mailto:")]/@href').get()
yield {'Link': link,'Text': text,'Email': email}
我在 Anaconda 提示符下運行以下命令列來運行上面的 Spider 并將其保存為 json 檔案
scrapy crawl tripadvisorbot -O tripadvisor.json
沒有資料被抓取,一個 json 檔案被創建但它是空的。
我不確定問題出在哪里,一般來說,我對網路抓取和 Python 編碼還是很陌生。所有幫助將不勝感激
謝謝
uj5u.com熱心網友回復:
在我的計算機上沒有類_cellContainer--和__restaurantName--HTML。
頁面使用隨機字符作為類名。
但是每個專案都直接在 div 中<div data-test-target="restaurants-list">,我用它來獲取所有專案。
后來我得到第一個<a>(它有影像而不是)name,我跳過但直接運行。textcomplete_urlreponse.follow(link)
當我得到詳細資訊的頁面時,我會reponse.url得到complete_url并h1得到text
您可以將所有代碼放在一個檔案中并在python script.py不創建專案的情況下運行。
import scrapy
class RestaurantSpider(scrapy.Spider):
name = 'tripadvisorbot'
start_urls = [
'https://www.tripadvisor.com/Restaurants-g188633-The_Hague_South_Holland_Province.html#EATERY_OVERVIEW_BOX'
]
def parse(self, response):
for listing in response.xpath('//div[@data-test-target="restaurants-list"]/div'):
url = listing.xpath('.//a/@href').get()
print('link:', url)
if url:
yield response.follow(url, callback=self.parse_listing)
next_url = response.xpath('//*[contains(@class,"pagination")]/*[contains(@class,"next")]/@href').get()
if next_url:
yield response.follow(next_url)
def parse_listing(self, response):
print('url:', response.url)
link = response.url
text = response.xpath('//h1[@data-test-target]/text()').get()
email = response.xpath('//a[contains(@href, "mailto:")]/@href').get()
yield {'Link': link, 'Text': text, 'Email': email}
# --- run without project and save data in `output.json` ---
from scrapy.crawler import CrawlerProcess
c = CrawlerProcess({
'USER_AGENT': 'Mozilla/5.0',
'FEEDS': {'output.json': {'format': 'json'}}, # new in 2.1
})
c.crawl(RestaurantSpider)
c.start()
部分結果:
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d4766834-Reviews-Bab_mansour-The_Hague_South_Holland_Province.html", "Text": "Bab mansour", "Email": null},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d3935897-Reviews-Milos-The_Hague_South_Holland_Province.html", "Text": "Milos", "Email": null},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d10902380-Reviews-Nefeli_deli-The_Hague_South_Holland_Province.html", "Text": "Nefeli deli", "Email": "mailto:[email protected]?subject=?"},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d8500914-Reviews-Waterkant-The_Hague_South_Holland_Province.html", "Text": "Waterkant", "Email": "mailto:[email protected]?subject=?"},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d4481254-Reviews-Salero_Minang-The_Hague_South_Holland_Province.html", "Text": "Salero Minang", "Email": null},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d6451334-Reviews-Du_Passage-The_Hague_South_Holland_Province.html", "Text": "Du Passage", "Email": "mailto:[email protected]?subject=?"},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d4451714-Reviews-Lee_s_Garden-The_Hague_South_Holland_Province.html", "Text": "Lee's Garden", "Email": null},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d2181693-Reviews-Warunee-The_Hague_South_Holland_Province.html", "Text": "Warunee", "Email": "mailto:[email protected]?subject=?"},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d8064876-Reviews-Sallo_s-The_Hague_South_Holland_Province.html", "Text": "Sallo's", "Email": "mailto:[email protected]?subject=?"},
{"Link": "https://www.tripadvisor.com/Restaurant_Review-g188633-d16841532-Reviews-Saravanaa_Bhavan_Den_Haag-The_Hague_South_Holland_Province.html", "Text": "Saravanaa Bhavan Den Haag", "Email": "mailto:[email protected]?subject=?"},
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448276.html
上一篇:pythonBeautifulSoupwebScraping輸出沒有寫入資訊
下一篇:使用scrapy清空結果檔案
