鱼C论坛

 找回密码
 立即注册
查看: 2683|回复: 2

python效率革命的问题

[复制链接]
发表于 2022-2-26 09:50:38 | 显示全部楼层 |阅读模式
15鱼币
本帖最后由 Ash~ 于 2022-2-26 10:34 编辑

屏幕截图 2022-02-26 094956.png 我已经照着打代码,也可以运行,但是没有结果

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-26 09:56:10 | 显示全部楼层
建议你把代码复制上来,别人好判断你是什么问题?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-26 10:52:28 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-26 12:02 编辑

因为没有设置一下请求头,所以被禁止访问了。你可以把代码改成下面这样:
  1. import requests
  2. import bs4
  3. headers = {'User-Agent': 'Mozilla/5.0'}      # 自定义请求头
  4. res = requests.get('https://movie.douban.com/top250', headers=headers)  # 访问的时候使用自定义请求头
  5. soup = bs4.BeautifulSoup(res.text, "html.parser")
  6. targets = soup.find_all('div', class_='hd')
  7. for each in targets:
  8.     print(each.a.span.text)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 23:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表