requests.get()状态码418

需要添加头,浏览器

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

import requests
import bs4

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
url = "http://movie.douban.com/top250"
res = requests.get(url, headers=headers)

soup = bs4.BeautifulSoup(res.text, "html.parser")

targets = soup.find_all("div", class_="hd")

for each in targets:
    print(each.a.span.text)

 

你可能感兴趣的:(Python3)