大地郡主 发表于 2020-7-27 23:03:15

爬豆瓣被挡

刚才再练习爬豆瓣,可是出现了ValueError: Invalid header name b'User:Agent'这个报错,上网查了下,说是在网页的headers里面有东西前面加了冒号,请问怎样把这个冒号删掉啊
import bs4
import re
import requests

header = {'User:Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
res = requests.get('https://movie.douban.com/top250',headers = header)
soup = bs4.BeautifulSoup(res.text,'html.parser')
namelist = soup.find_all('div',class_='hd')
for each in namelist:
    print(each.a.span.text)

Twilight6 发表于 2020-7-27 23:04:39




是 User-Agent 不是 User:Agent

import bs4
import re
import requests

header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
res = requests.get('https://movie.douban.com/top250',headers = header)
soup = bs4.BeautifulSoup(res.text,'html.parser')
namelist = soup.find_all('div',class_='hd')
for each in namelist:
    print(each.a.span.text)

大地郡主 发表于 2020-7-27 23:26:39

Twilight6 发表于 2020-7-27 23:04
是 User-Agent 不是 User:Agent

呃,低级错误{:10_247:}谢谢您,我再认真些
页: [1]
查看完整版本: 爬豆瓣被挡