鱼C论坛

 找回密码
 立即注册
查看: 1451|回复: 4

[已解决]爬取豆瓣250字符转义问题

[复制链接]
发表于 2021-10-6 14:07:47 | 显示全部楼层 |阅读模式
5鱼币
微信图片_20211006140024.png 微信图片_20211006140014.png
求助大神——
如图,我想把中英文标题都爬取下来,但是两个标题中间有一个斜杠,我试着用replace拿掉,结果还是乱码,这是啥问题?
最佳答案
2021-10-6 14:07:48
  1. import requests
  2. from bs4 import BeautifulSoup as bs
  3. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}
  4. res = requests.get('https://movie.douban.com/top250', headers = headers)
  5. soup = bs(res.text, 'html.parser')
  6. movies = []
  7. targets = soup.find_all('div', class_ ='hd')
  8. for each in targets:
  9.     for sp in each.find_all('span', class_='title'):
  10.         movies.append(sp.text.replace(' / ', ''))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-6 14:07:48 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. from bs4 import BeautifulSoup as bs
  3. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}
  4. res = requests.get('https://movie.douban.com/top250', headers = headers)
  5. soup = bs(res.text, 'html.parser')
  6. movies = []
  7. targets = soup.find_all('div', class_ ='hd')
  8. for each in targets:
  9.     for sp in each.find_all('span', class_='title'):
  10.         movies.append(sp.text.replace(' / ', ''))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-6 16:27:37 | 显示全部楼层

谢谢大神~请问这个 / 是什么意思,和网页源码&nbsp是什么转换关系呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-6 16:32:18 | 显示全部楼层
翰桑曼 发表于 2021-10-6 16:27
谢谢大神~请问这个 / 是什么意思,和网页源码&nbsp是什么转换关系呢?

xml格式中的空格
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-6 16:41:50 | 显示全部楼层

好的~十分感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 11:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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