我想使用scrapy在網站https://www.germanystartupjobs.com
上發佈所有工作。由於通過POST請求加載的作業,我把start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/']
。我在使用Chrome dev tool
的命令method:POST
的network
選項卡的第1頁中找到此URL
。如何查找網站上列出的所有工作?
我認爲在第二頁,我會得到不同的URL
但是,在這裏似乎不是這種情況。我也試過用
start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/' + str(i) for i in range(1, 5)]
生成更多的頁面與索引沒有幫助。我的代碼的當前版本在這裏:
import scrapy
import json
import re
import textwrap
class GermanyStartupJobs(scrapy.Spider):
name = 'gsjobs'
start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/' + str(i) for i in range(1, 5)]
def parse(self, response):
data = json.loads(response.body)
html = data['html']
selector = scrapy.Selector(text=data['html'], type="html")
hrefs = selector.xpath('//a/@href').extract()
print "LENGTH = ", len(hrefs)
for href in hrefs:
yield scrapy.Request(href, callback=self.parse_detail)
def parse_detail(self, response):
try:
full_d = str(response.xpath\
('//div[@class="col-sm-5 justify-text"]//*/text()').extract())
full_des_li = full_d.split(',')
full_des_lis = []
for f in full_des_li:
ff = "".join((f.strip().replace('\n', '')).split())
if len(ff) < 3:
continue
full_des_lis.append(f)
full = 'u'+ str(full_des_lis)
length = len(full)
full_des_list = textwrap.wrap(full, length/3)[:-1]
full_des_list.reverse()
# get the job title
try:
title = response.css('.job-title').xpath('./text()').extract_first().strip()
except:
print "No title"
title = ''
# get the company name
try:
company_name = response.css('.company-title').xpath('./normal/text()').extract_first().strip()
except:
print "No company name"
company_name = ''
# get the company location
try:
company_location = response.xpath('//a[@class="google_map_link"]/text()').extract_first().strip()
except:
print 'No company location'
company_location = ''
# get the job poster email (if available)
try:
pattern = re.compile(r"(\w(?:[-.+]?\w+)+\@(?:[a-z0-9](?:[-+]?\w+)*\.)+[a-z]{2,})", re.I)
for text in full_des_list:
email = pattern.findall(text)[-1]
if email is not None:
break
except:
print 'No email'
email = ''
# get the job poster phone number(if available)
try:
r = re.compile(".*?(\(?\d{3}\D{0,3}\d{3}\D{0,3}\d{4}).*?", re.S)
phone = r.findall(full_des_list[0])[-1]
if phone is not None:
phone = '+49-' +phone
except:
print 'no phone'
phone = ''
yield {
'title': title,
'company name': company_name,
'company_location': company_location,
'email': email,
'phone': phone,
'source': u"Germany Startup Job"
}
except:
print 'Not valid'
# raise Exception("Think better!!")
我想從網站的至少17頁獲得類似信息。我怎麼能做到這一點,並改善我的代碼?獲得所需信息後,我計劃使用multi-threading
加快此過程,並且nltk
搜索海報名稱(如果可用)。
我曾與整個部門的人誰的工作是寫好蜘蛛/ scrapers。我不完全確定這是有限的範圍,是一個很好的SO問題。 –
感謝您的回答。正如你所看到的,我可以從第一頁獲得信息,並需要找出獲得其餘頁面的方法。如果您有經驗,相同的示例代碼或解釋將非常有幫助。我需要在今天完成這項工作 – Chak