import requests from lxml import etree import pandas as pd headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' } url = f'http://b2b.huangye88.com/shenzhen/jixie/pn2/' res = requests.get(url, headers=headers) res.encoding = 'utf-8' html = etree.HTML(res.text) for et in html.xpath('//*[@id="jubao"]/dl'): name = et.xpath('./dt/h4/a/@title') print(name[0]) hrefs = [] href = et.xpath("./dt/h4/a/@href")[0] hrefs.append(href) content = [] # 保存内容 for u in hrefs: msg = requests.get(u).text xmsg = etree.HTML(msg) if len(name) != 0: tel = xmsg.xpath('/ul[@class ="1-txt none"/li') if len(tel) != 0: tel_name = tel.xpath('.//label/text()') if len(tel_name) != 0: tel_iphone=tel.xpath('.//span[@class="secret"]/text()') if len(tel_iphone) != 0: tel = tel[0] print(name[0], tel_name,tel_iphone, '\n')
时间: 2024-04-22 18:25:44 浏览: 153
根据您提供的代码,我注意到您在进行xpath解析时出现了一些错误。以下是我发现的问题和修复建议:
1. 在`tel = xmsg.xpath('/ul[@class ="1-txt none"/li')`这行代码中,XPath表达式的括号未正确闭合。修改为`tel = xmsg.xpath('//ul[@class="1-txt none"]/li')`。
2. 在`tel = tel[0]`这行代码中,您将`tel`变量重新赋值为列表中的第一个元素。但是,在上一行代码中,您已经将`tel`变量用于判断是否存在电话信息。因此,在这里不需要重新赋值。
下面是修复后的代码片段:
```python
import requests
from lxml import etree
import pandas as pd
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}
url = f'http://b2b.huangye88.com/shenzhen/jixie/pn2/'
res = requests.get(url, headers=headers)
res.encoding = 'utf-8'
html = etree.HTML(res.text)
for et in html.xpath('//*[@id="jubao"]/dl'):
name = et.xpath('./dt/h4/a/@title')
print(name[0])
hrefs = []
href = et.xpath("./dt/h4/a/@href")[0]
hrefs.append(href)
content = [] # 保存内容
for u in hrefs:
msg = requests.get(u).text
xmsg = etree.HTML(msg)
if len(name) != 0:
tel = xmsg.xpath('//ul[@class="1-txt none"]/li')
if len(tel) != 0:
tel_name = tel.xpath('.//label/text()')
if len(tel_name) != 0:
tel_iphone=tel.xpath('.//span[@class="secret"]/text()')
if len(tel_iphone) != 0:
print(name[0], tel_name, tel_iphone, '\n')
```
请注意,这只是对您提供的代码片段进行了修复,并不能保证整个代码的正确性。如果您还遇到其他问题,请提供更多的信息,我将尽力帮助您解决。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)