Loop_Sern 2019-10-12 17:21 采纳率: 0%
浏览 4248

爬虫过程中遇到报错:ValueError: can only parse strings

源代码如下:
import requests
import json
from requests.exceptions import RequestException
import time
from lxml import etree

def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0(Macintosh;Intel Mac OS X 10_13_3) AppleWebKit/537.36(KHTML,like Gecko) Chorme/65.0.3325.162 Safari/537.36'
}
response = requests.get(url,headers = headers)
if response.status_code == 200:
return response.text
return None
except RequestException:
return None

def parse_one_page(html):
html_coner = etree.HTML(html)
pattern = html_coner.xpath('//div[@id="container"]/div[@id="main"/div[@class = "ywnr_box"]//a/text()')
return pattern

def write_to_file(content):
with open('results.txt','a',encoding='utf-8') as f:
f.write(json.dumps(content,ensure_ascii=False)+'\n')

def main(offset):
url = 'http://www.cdpf.org.cn/yw/index_'+str(offset)+'.shtml'
html = get_one_page(url)
for item in parse_one_page(html):
print(item)
write_to_file(item)

if name == '__main__':
for i in range(6):
main(offset=i*10)
time.sleep(1)
请问各位大佬到底是哪里出了错??

  • 写回答

1条回答

  • threenewbee 2019-10-12 17:37
    关注

    python代码依赖缩进,你贴出来的连缩进都没有。
    python给出的错误信息,同时会给出你错误的行号甚至行的内容,你也不给。
    自己仔细检查下吧,有一个地方要你给字符串,你给的变量不是字符串

    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败