【发布时间】:2015-03-11 07:03:11
【问题描述】:
问题陈述:
我在每行的文件名 myurls.csv 中有一个论坛网址列表,如下所示:
https://www.drupal.org/user/3178461/track
https://www.drupal.org/user/511008/track
我写了一个CrawlSpider代码来爬取论坛帖子如下:
class fileuserurl(CrawlSpider):
name = "fileuserurl"
allowed_domains = []
start_urls = []
rules = (
Rule(SgmlLinkExtractor(allow=('/user/\d/track'),
restrict_xpaths = ('//li[@class="pager-next"]',),
canonicalize=False ),callback='parse_page',follow=True)
)
def __init__(self):
f = open('./myurls.txt','r').readlines()
self.allowed_domains = ['www.drupal.org']
self.start_urls = [l.strip() for l in f]
super(fileuserurl,self).__init__()
def parse_page(self, response):
print '*********** START PARSE_PAGE METHOD**************'
# print response.url
items = response.xpath("//tbody/tr")
myposts=[]
for temp in items:
item = TopicPosts()
item['topic'] = temp.xpath(".//td[2]/a/text()").extract()
relative_url = temp.xpath(".//td[2]/a/@href").extract()[0]
item['topiclink'] = 'https://www.drupal.org'+relative_url
item['author'] = temp.xpath(".//td[3]/a/text()").extract()
try:
item['replies'] = str(temp.xpath(".//td[4]/text()").extract()[0]).strip('\n')
except:
continue
myposts.append(item)
return myposts
问题:
它只给了我文本文件中提到的 url 的第一页输出。我想转到首页中下一步定义的页面的每个链接。
【问题讨论】:
-
请修正你的缩进。
-
alecxe,我的编辑器(pycharm)中的缩进是正确的,它在stackoverflow中复制时唯一的错误
标签: python web-scraping scrapy forum