解决以下代码的错误: import requests from pyquery import PyQuery as pq def get_num(): headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36', 'Cookie' : 'Q_UDID=ce0129a0-bba3-6764-02e0-ca7893ab2c4f; sessionID=5a7917c3f25cf85fcb1ebf9ea5e53f0b' } data={ "page": "1", "page_num": "10", "search_type": "5" } url = 'https://xcsrc.360zqaq.net/api/backend/bugmanage/buglist ' res = requests.post(url,headers=headers,data=data,timeout=10) content = res.content.decode('utf-8') doc = pq(content) item = doc('.cell el-tooltip') for i in item: b = pq(i).text() print(b) if __name__ == '__main__': get_num()
时间: 2024-04-13 09:27:30 浏览: 150
requests-random-user-agent:配置请求库以随机选择桌面用户代理
你的代码有一个错误。在使用 PyQuery 进行选择器操作时,需要使用 `pq()` 函数来创建一个 PyQuery 对象,然后再使用选择器进行操作。所以,你需要将 `item = doc('.cell el-tooltip')` 修改为 `item = doc('.cell.el-tooltip')`。此外,你还需要修改 `item` 的遍历方式,将 `for i in item:` 修改为 `for i in item.items():`。以下是修改后的代码:
```python
import requests
from pyquery import PyQuery as pq
def get_num():
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36',
'Cookie' : 'Q_UDID=ce0129a0-bba3-6764-02e0-ca7893ab2c4f; sessionID=5a7917c3f25cf85fcb1ebf9ea5e53f0b'
}
data={
"page": "1", "page_num": "10", "search_type": "5"
}
url = 'https://xcsrc.360zqaq.net/api/backend/bugmanage/buglist'
res = requests.post(url, headers=headers, data=data, timeout=10)
content = res.content.decode('utf-8')
doc = pq(content)
items = doc('.cell.el-tooltip')
for i in items.items():
b = pq(i).text()
print(b)
if __name__ == '__main__':
get_num()
```
这样修改后,你应该能够正确地获取到文本信息并打印出来了。
阅读全文