2021-02-21

Traceback (most recent call last):
  File "F:/python_code/Shujujiexi/58Tongcheng.py", line 17, in <module>
    file.write(result)
TypeError: write() argument must be str, not list

解决办法

import  requests
from lxml import etree
request_headers ={
     
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74'
}

url = 'https://xy.58.com/ershoufang/'
response = requests.get(url=url,headers=request_headers).text
tree = etree.HTML(response)
list = tree.xpath('//section[@class="list"]/div/a/div')
file = open('58.txt','w',encoding='utf-8')
l =[]
for info in list:
    result = info.xpath('./div[1]/div[1]/h3/text()')
    if len(result) != len(l): //我在这里加入了一个筛选机制
     print(result)
     file.write(result[0]+'\n') //原来write只能接收字符型数据,因此需要通过下标的形式提取出数据,

你可能感兴趣的:(笔记,python)