
请问这是什么原因呢,我不会爬虫,自学的看不出来错误是什么,怎么解决呢,来个懂得老哥解惑吧
关注【以下回答由 GPT 生成】
I can help you with this issue.
The error `TypeError: argument for open() given by name ('mode') and position (2)` is due to the incorrect usage of `open()` function. The `open()` function takes in the file name, mode, and encoding as its arguments.
Here's the corrected code:
```python
import requests
import re
url = "DDLL" # 请求链接
headers = {"user-agent": "PyCharm Community Edition 2021.3.2 Moz11la/5.0 (Windows NT 19.0; Win64; x64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"} # 模拟浏览器headers请求头
response = requests.get(url=url, headers=headers) # 发送请求
title = re.findall('<a href="javascript:;>(.*?)</a>', response.text) # 提取内容
print(title)
content = re.findall('<dd class="infor_dd1">(.*?)</dd>', response.text)
print(content)
with open("title.txt", "a", encoding="utf-8") as f:
f.write(str(title)) # Convert list to string before writing
f.write('\n')
f.write(str(content)) # Convert list to string before writing
f.write('\n')
In the corrected code, I have added str() to convert the title and content lists into strings before writing them to the file.
Please try running the corrected code and it should resolve the TypeError issue.
```
【相关推荐】