wzdyjn 发表于 2020-7-13 15:01:33

requests请求问题

import requests
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

fenlei_url="https://movie.douban.com/j/search_tags?type=tv&source="
response =requests.get(fenlei_url)
rel =response.content.decode()
print(rel )

没有打印结果 不知道为什么?去掉ssl验证又报错郁闷这个网址直接在浏览器中是有结果的那个大神榜我看看

Twilight6 发表于 2020-7-13 15:11:51



加个 headers 参数就好:

import requests
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

fenlei_url="https://movie.douban.com/j/search_tags?type=tv&source="
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response =requests.get(fenlei_url,headers=headers)
rel =response.content.decode()
print(rel)

输出结果:{"tags":["热门","美剧","英剧","韩剧","日剧","国产剧","港剧","日本动画","综艺","纪录片"]}

青出于蓝 发表于 2020-7-13 15:14:30

本帖最后由 青出于蓝 于 2020-7-13 15:16 编辑

import requests
cookie={"Cookie":"""bid=mNQ7A7viJzI; douban-fav-remind=1; __gads=ID=4b7e04ab147c12ab:T=1591421016:S=ALNI_MY_zPkvb5MuCioC42Ca0KhJUgyE5w; __utma=30149280.116605.1591421020.1592541189.1592731495.3; __utmz=30149280.1592541189.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1592731495%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DSVTZxJFNs0ZG-oePWrjV7189CAPCK0k76H35o4qQdQmKyNuqU3_N3jPsGAGCpBZo%26wd%3D%26eqid%3D8158995500000a15000000025eec3ffe%22%5D; _pk_id.1
      00001.4cf6=371d7c14bd1cf098.1592541189.2.1592731495.15925412
      15.; __yadk_uid=uNUP8kIVFu9QtVYEPvOlA7KXvZQvZb3v; __utma=2236
      95111.1353623826.1592541189.1592541189.1592731496.2; __utmz=2
      23695111.1592541189.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=
      organic; ll="118220"; _vwo_uuid_v2=D8BB91EF5285B2E6D599A2D7BE
      04BEAED|caf2d36e4b3a50344438a00d8947acae"""}
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0"}
fenlei_url="https://movie.douban.com/j/search_tags?type=tv&source="
response=requests.get(fenlei_url,cookies=cookie,headers=headers).text
print(response)

结果:
{"tags":["热门","美剧","英剧","韩剧","日剧","国产剧","港剧","日本动画","综艺","纪录片"]}

青出于蓝 发表于 2020-7-13 15:15:41

ssl不需要啦,加个user-agent就好
还给加了个cookie,有了cookie,就等于登录进去了

小甲鱼的铁粉 发表于 2020-7-13 15:43:40

还是爬虫有趣{:10_256:}

wzdyjn 发表于 2020-7-13 19:54:48

谢谢 亲们

wzdyjn 发表于 2020-7-16 19:26:36

青出于蓝 发表于 2020-7-13 15:14
结果:

cookie 还可以这样写   三引号    学到了
页: [1]
查看完整版本: requests请求问题