import scrapy
class ZySpider(scrapy.Spider):
name = 'zy'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.mee.gov.cn/zcwj/zyygwj/']
def parse(self, response):
li_list=response.xpath('//*[@id="div"]/li')
for li in li_list:
new_url='https://www.mee.gov.cn/zcwj/zyygwj/'+li.xpath('./a/@href').extract_first()
new_name=li.xpath('./a/text()').extract_first()
new_date=li.xpath('./span/text()').extract_first()
print(new_url,new_name,new_date)
# 提取下一页的href并拼接url
next_url = response.xpath('//a[text()="下一页"]/@href').extract_first()
# 判断是否是最后一页
if next_url != 'javascript:;':
next_url = response.urljoin(next_url)
yield scrapy.Request(next_url, callback=self.parse)
xpath得出尾页的index_数字多少.然后翻页就是从1到 尾页这样加,拼URL链接
应该是他的下一页每页带前面的链接.只有index_3.shtml 你得加前缀吧
我这边用我的翻页方法是正常爬取数据的
https://www.mee.gov.cn/zcwj/zyygwj/index_%s.shtml 你把总页数通过XPATH然后直接正则取出.拼接URL
除非网页特殊情况.一般我是把翻页的所有URL直接拼出来凑成集合.而不是一页一页获取URL....