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给出的错误信息,同时会给出你错误的行号甚至行的内容,你也不给。
    自己仔细检查下吧,有一个地方要你给字符串,你给的变量不是字符串

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?