Tihool 发表于 2022-4-30 13:30:18

爬虫requests

import requests
def main():
    url ='https://news.house.qq.com/a/20170702/003985.htm'
    res = open_url(url)
    with open('房价.txt','w',encoding= 'utf-8') as f:
      f.write(res.txt)

def open_url(url):
    head = {
      "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
    }
    response = requests.get(url, headers=head)

    return response
if __name__ == "__main__":
    main()
为什么我的requests.get只能得到网页状态码,而小甲鱼的代码又能得到网页源码,不理解

Twilight6 发表于 2022-4-30 13:51:59


写入文件调用的应该是 .text 吧,你写成 .txt 了

f.write(res.text)
页: [1]
查看完整版本: 爬虫requests