|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
<code>
# -*- coding: utf-8 -*-
import scrapy
from twlk.items import TwlkItem
class MoviesSpider(scrapy.Spider):
name = 'movies'
#allowed_domains = ['twlkbt.com']
def start_requests(self):
start_urls = ['http://twlkbt.com/forum-2-%s.html' %str(x) for x in range(1,3)]
#print(start_urls)
for url in start_urls:
yield scrapy.Request(url,self.parse)
def parse(self, response):
# GET SINGLE_PAGE URL
for href in response.xpath('//a[@class=\'xst\']/@href'):
yield response.follow(href,self.parse2)
def parse2(self,response):
item = TwlkItem()
#get torrent link
src_link = response.xpath("//span[@onmouseover]/a/@href")
print('外部src_link是:',src_link)
item['title'] = response.xpath("//*[@id='thread_subject']/text()").extract_first()
#title = title.replace('/','-')
#title = title.replace(':',' ')
#img_url = response.xpath("//img[@onclick='zoom(this, this.src, 0, 0, 0)']/@src")
#print(img_url)
print('title======',item['title'])
if src_link:
print('第一个if')
src_link = response.xpath("//span[@onmouseover]/a/@href").extract_first()
print("现在的src_link是",src_link)
item['src_link'] = 'http://twlkbt.com/' + src_link
print("现在的item['src_link']是",item['src_link'])
yield item
print("yiele ok")
else:
print('进入else块--')
src_link = response.xpath("//a[@onmouseover=\"showMenu({'ctrlid':this.id,'pos':'12'})\"]/@href").extract_first()
print("现在的src_link是",src_link)
item['src_link'] = 'http://twlkbt.com/' + src_link
print("现在的item['src_link']是",item['src_link'])
yield item
print('yield ok')
#下载Torrent
</code>
结果在 前面一部分还没问题src_link正常的解析结果只有一个。 但是程序运行到中间 后面的部分 src_link就解析出2个元素了 所以导致结果出现问题
想了很久 想不到为什么同一个解析语句 到后面结果会一个变2个 (特别用scrapy shell 分别就结果正常网址 与 结果不正常网址做了测试 发现在shell里 同一个解析语句 解析结果又没问题 ,。。所以。。程序有问题?看不出来 囧。。。)
下面是中间一部分不同结果运行信息 :
018-11-25 20:05:42 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://twlkbt.com/thread-86371-1-1.html> (referer: http://twlkbt.com/forum-2-1.html) ['partial']
外部src_link是: [<Selector xpath='//span[@onmouseover]/a/@href' data='forum.php?mod=attachment&aid=NDg2MjF8N2E'>]
title====== [11.24][中国][动作][新七侠五义之屠龙案][WEB.1080p-MKV/2G][国语中字][2018新片]
第一个if
现在的src_link是 forum.php?mod=attachment&aid=NDg2MjF8N2EzOTMwZTF8MTU0MzE0NzU0MHwwfDg2Mzcx
现在的item['src_link']是 http://twlkbt.com/forum.php?mod=attachment&aid=NDg2MjF8N2EzOTMwZTF8MTU0MzE0NzU0MHwwfDg2Mzcx
2018-11-25 20:05:42 [scrapy.core.scraper] DEBUG: Scraped from <200 http://twlkbt.com/thread-86371-1-1.html>
{'src_link': 'http://twlkbt.com/forum.php?mod=attachment&aid=NDg2MjF8N2EzOTMwZTF8MTU0MzE0NzU0MHwwfDg2Mzcx',
'title': '[11.24][中国][动作][新七侠五义之屠龙案][WEB.1080p-MKV/2G][国语中字][2018新片]'}
yiele ok
2018-11-25 20:05:43 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://twlkbt.com/thread-86117-1-1.html> (referer: http://twlkbt.com/forum-2-1.html) ['partial']
外部src_link是: [<Selector xpath='//span[@onmouseover]/a/@href' data='forum-37-1.html'>, <Selector xpath='//span[@onmouseover]/a/@href' data='forum.php?mod=attachment&aid=NDgzNDB8Mzg'>, <Selector xpath='//span[@onmouseover]/a/@href' data='forum-37-1.html'>]
title====== [10.18][美国][动作][第九禁区][高清BluRay.1080p-MKV/7G][国英双语中字][经典刺激]
第一个if
现在的src_link是 forum-37-1.html
现在的item['src_link']是 http://twlkbt.com/forum-37-1.html
2018-11-25 20:05:43 [scrapy.core.scraper] DEBUG: Scraped from <200 http://twlkbt.com/thread-86117-1-1.html>
{'src_link': 'http://twlkbt.com/forum-37-1.html',
'title': '[10.18][美国][动作][第九禁区][高清BluRay.1080p-MKV/7G][国英双语中字][经典刺激]'}
yiele ok
怎么说好呢??
我不是通过网页审查元素写的xpath语句。
我直接阅读网页源代码写的xpath语句。
解析方法,像bs4、re、pq、xpath、pandas 等,有好几种变化,每个人写出来,有34种变化。
变化的原因一般在网页源代码中。
应该是由于这个网页有一点 动态加载吧。
|
|