鱼C论坛

 找回密码
 立即注册
查看: 1386|回复: 3

scrapy报错

[复制链接]
发表于 2021-8-31 15:32:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import scrapy


  2. class BpictureSpider(scrapy.Spider):
  3.     name = 'bpicture'
  4.     allowed_domains = ['www.qiushibaike.com']
  5.     start_urls = ['https://www.qiushibaike.com/text/']

  6.     def parse(self, response):
  7.         #解析
  8.         div_list = response.xpath('//*[@class="col1 old-style-col1"]/div/div[2]')
  9.         for div in div_list:
  10.             author = div.xpath('./div[1]/a[2]/h2/text()')[0].extract()

  11.             content = div.xpath('./a/div/span//text()').extract_first()
  12.             content = ''.join(content)

  13.             print(author, content)
  14.             break
复制代码

错误结果如图:
尝试过将list[0].extract()改成list().extract_first()  还是一样的结果
屏幕截图 2021-08-31 153014.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-31 15:40:43 | 显示全部楼层
在15行后面加上print(content),看看content的内容,错误的提示是只能join可迭代的对象。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-31 16:56:01 | 显示全部楼层
冬雪雪冬 发表于 2021-8-31 15:40
在15行后面加上print(content),看看content的内容,错误的提示是只能join可迭代的对象。
  1. import scrapy


  2. class BpictureSpider(scrapy.Spider):
  3.     name = 'bpicture'
  4.     allowed_domains = ['www.xxx.com']
  5.     start_urls = ['https://www.qiushibaike.com/text/']

  6.     def parse(self, response):
  7.         #解析
  8.         div_list = response.xpath('//*[@class="col1 old-style-col1"]/div')
  9.         #response.xpath('//div[@class="col1 old-style-col1"]')
  10.         for div in div_list:
  11.             author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()

  12.             content = div.xpath('./a/div/span//text()').extract()
  13.             print(content)
  14.             content = ''.join(content)

  15.             print(author, content)
  16.             break
复制代码

我先将author和content的extract改成了现在的样子,返回的结果是None
然后我有将div_list的路径修改呈现在的样子,成功了
屏幕截图 2021-08-31 164950.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-31 20:34:17 | 显示全部楼层
我scrapy用的很少,用bs写了一个供参考。
  1. import requests
  2. from bs4 import BeautifulSoup
  3. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"}
  4. res = requests.get('https://www.qiushibaike.com/text/', headers = headers)
  5. soup = BeautifulSoup(res.text, 'html.parser')
  6. targets = soup.find_all('div', class_ = 'content')
  7. for each in targets:
  8.     print(each.span.text)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 09:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表