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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题