我想使用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