[英]How to scrape element form scraped url? Scrapy
好的,假設我有一個列出工作機會的網站,並且有多個頁面(動態的,這就是我使用 selenium 的原因)。 我想做的事:
class JobScraper(scrapy.Spider):
name = "jobscraper"
allowed_domains = ['pracuj.pl']
total = 10
start_urls = [
'https://www.pracuj.pl/praca/it%20-%20rozw%c3%b3j%20oprogramowania;cc,5016/%c5%82%c3%b3dzkie;r,5?rd=10&pn={}'.format(i)
for i in range(1, total)
]
custom_settings = {
'LOG_LEVEL': 'INFO',
}
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.headless = True
self.driver = webdriver.Chrome(r'C:\Users\kacpe\OneDrive\Pulpit\Python\Projekty\chromedriver.exe',
options=self.options)
def parse(self, response):
self.driver.get(response.url)
res = response.replace(body=self.driver.page_source)
offers = res.xpath('//li[contains(@class, "results__list-container")]')
for offer in offers:
link = offer.xpath('.//a[@class="offer-details__title-link"]/@href').extract()
yield Request(link, callback=self.parse_page)
def parse_page(self, response):
title = response.xpath('//h1[@data-scroll-id="job-title"]/text()').extract()
yield {
'job_title': title
}
而且它不起作用,發生了一個錯誤:
TypeError: 請求 url 必須是 str 或 unicode,得到列表
您在這一行中調用extract
:
link = offer.xpath('.//a[@class="offer-details__title-link"]/@href').extract()
Extract 返回一個元素列表,這就是您嘗試將link
傳遞給Request
時收到錯誤的原因。
根據您想要執行的操作,您可以for link in links
執行操作並Request
每個結果,或者使用find_elements_by_xpath
xpath
您不需要 selenium 來抓取所需的內容。 事實證明,您希望從該站點獲取的項目位於某個腳本標記中。 一旦您使用正則表達式挖出該部分並使用 json 庫處理它,您應該非常輕松地訪問它們。 以下是我的意思:
import json
import scrapy
class JobScraper(scrapy.Spider):
name = "jobscraper"
total = 10
start_urls = [
'https://www.pracuj.pl/praca/it%20-%20rozw%c3%b3j%20oprogramowania;cc,5016/%c5%82%c3%b3dzkie;r,5?rd=10&pn={}'.format(i)
for i in range(1, total)
]
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
}
def start_requests(self):
for start_url in self.start_urls:
yield scrapy.Request(start_url,callback=self.parse,headers=self.headers)
def parse(self, response):
items = response.css("script:contains('window.__INITIAL_STATE__')::text").re_first(r"window\.__INITIAL_STATE__ =(.*);")
for item in json.loads(items)['offers']:
yield {
"title":item['jobTitle'],
"employer":item['employer'],
"country":item['countryName'],
"details_page":item['companyProfileUrl']
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.