我是新来的scrapy,但使用python一段时间。我从scrapy
文档以及xpath
选择器中吸取了教训。现在,我想将知识转化为一个小项目。我正尝试使用scrapy从作业板https://www.germanystartupjobs.com/
中取消job links and the associated info like job title, location, emails (if any), phone numbers (if any)
。如何使用scrapy获取href和相关信息?
我有这样的起动代码,
import scrapy
class GermanSpider(scrapy.Spider):
# spider name
name = 'germany'
# the first page of the website
start_urls= ['https://www.germanystartupjobs.com/']
print start_urls
def parse(self, response):
pass
def parse_detail(self, response):
pass
和运行蜘蛛scrapy runspider germany
里面的parse
功能,我想获得的parse_detail
函数内href
S和细节。
的时候,我跟chrome
开发工具打开提到的页面,并检查列出的作业,我看到所有的工作都是在这里面ul
<ul id="job-listing-view" class="job_listings job-listings-table-bordered">
,然后,将中隔离作业中所列出的许多的
<div class="job-info-row-listing-class">
与关联的相关信息divs
,也就是说,href
的内部设置<a href="https://www.germanystartupjobs.com/job/foodpanda-berlin-germany-2-sem-manager-mf/">
其他divs
提供职位,公司名称,地址等与divs
如
<div>
<h4 class="job-title-class">
SEM Manager (m/f) </h4>
</div>
<div class="job-company-name">
<normal>foodpanda<normal> </normal></normal></div>
</div>
<div class="location">
<div class="job-location-class"><i class="glyphicon glyphicon-map-marker"></i>
Berlin, Germany </div>
</div>
的第一步将是获得href
使用parse
功能,然后,将parse_details
内的相关信息使用response
。我发现email
和phone
号码仅在您打开href
的链接时提供,但标题和位置在同一页的当前divs
内提供。
正如我所提到的,我在Python中编程的技巧还不错,但是,即使在使用tutorial之后,我仍然在使用xpath
s。如何找到链接和相关信息?一些示例代码很少有解释,将会有所帮助。
我尝试使用代码
# firstly
for element in response.css("job-info-row-listing-class"):
href = element.xpath('@href').extract()[0]
print href
yield scrapy.Request(href, callback=self.parse_detail)
# secondly
values = response.xpath('//div[@class="job-info-row-listing-class"]//a/text()').extract()
for v in values:
print v
#
values = response.xpath('//ul[@id="job-listing-view"]//div[@class="job-info-row-listing-class"]//a/text()').extract()
他们似乎很容易使用乳宁scrapy runspider germany
感谢您的回答,这真的很有帮助。我发布了另一个与'scrapy'相关的问题,可能会对你有所帮助'http:// stackoverflow.com/questions/41178659/how-to-get-the-job-description-using-scrapy' – Chak