第六行代码的引号位置打错了
import requests
import bs4
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'cookie': 'BIDUPSID=45588421F978DE2902A1A47464D72AB3; PSTM=1609986056; BAIDUID=45588421F978DE29E3D21BB7BB5A6873:FG=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; __yjs_duid=1_e3607a56e43709e2ca3db4147c46396a1612784778582; BAIDUID_BFESS=45588421F978DE29E3D21BB7BB5A6873:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1614696427; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1614697139; __yjsv5_shitong=1.0_7_253e552513b2cf371866473b2e57fb4e5137_300_1614697145680_59.50.85.17_fe15699c; ab_sr=1.0.0_ZDRiZDU1NjAzNjRkMDUyOWE3ZTg3MWZlMjRhMGE4NzkwYmI3N2ExMzliMjY3MjIxOGIyY2EyMmMxNWQzOWFmY2UyZDZjMGFkYzVjNWZkNGQ1ZWIxMTJmYjZjM2Y0M2M2'}
res = requests.get('https://movie.douban.com/top250',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)
|