2301_81598855 2023-12-03 22:17 采纳率: 0%
浏览 3

python爬虫运行问题求解惑

img


请问这是什么原因呢,我不会爬虫,自学的看不出来错误是什么,怎么解决呢,来个懂得老哥解惑吧

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-04 09:13
    关注

    【以下回答由 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. ```



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 12月3日