问题遇到的现象和发生背景
用python爬取网页时,拼接url类型错误,运行后显示拼接的那段url需要转换为str类型,但是如果强制转换后输出的url就会带有[],是无效的url,就不能继续进行对新的url的解析了
问题相关代码,请勿粘贴截图
import requests
from lxml import html
etree=html.etree
if __name__ == '__main__':
url="https://www.shicimingju.com/book/sanguoyanyi.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36"}
page_text=requests.get(url,headers).content.decode('utf-8')
tree=etree.HTML(page_text)
li_link=tree.xpath('//div[@class="book-mulu"]/ul/li')
fp = open('./三国.txt', 'w', encoding='utf-8')
for link in li_link:
#拼接错误!!!!!!
#li_url="https://www.shicimingju.com"+str(link.xpath('./a/@href'))
li_url = "https://www.shicimingju.com" + link.xpath('./a/@href')
print(li_url)
title=link.xpath('./a/text()')
detail_text=requests.get(url=li_url,headers=headers).content.decode('utf-8')
tree = etree.HTML(detail_text)
content=tree.xpath('//div[@class="card bookmark-list"]/div/text()')
fp.write(str(title+content))
# print(content,"结束")
print(title,'成功!!')
运行结果及报错内容
```
```li_url = "https://www.shicimingju.com" + link.xpath('./a/@href')
TypeError: can only concatenate str (not "list") to str
我的解答思路和尝试过的方法
加str强制转换
我想要达到的结果
拼接生成正确的url