鱼C论坛

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

[已解决]关于beautifulsoup的问题

[复制链接]
发表于 2017-9-8 21:16:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 新手·ing 于 2017-9-8 21:19 编辑

网址:http://www.tfls.cn/html/students/union/

我想爬取
(~TI4VS9{LFV@X0D(QR`6CH.png
图片中的链接

因为新手比较渣,,所以失败了
  1. import requests
  2. from bs4 import BeautifulSoup

  3. doc = requests.get('http://www.tfls.cn/html/students/union/').content
  4. soup = BeautifulSoup(doc, 'html.parser')
  5. for link in soup.find_all('a'):
  6.     print(link.get('href'))
复制代码

这样爬出来的会有多余的,我不想要的,,
求改正
最佳答案
2017-9-8 23:37:43
本帖最后由 ButcherRabbit 于 2017-9-9 00:06 编辑
  1. import requests
  2. from bs4 import BeautifulSoup

  3. doc = requests.get('http://www.tfls.cn/html/students/union/').content
  4. soup = BeautifulSoup(doc,'html.parser')
  5. for link in soup.find_all(valign="middle"):
  6.     for link1 in link.find_all('a'):
  7.         print('http://www.tfls.cn' + link1.get('href'))
复制代码


http://cuiqingcai.com/1319.html
先用keyword 参数筛选一遍。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-8 23:37:43 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ButcherRabbit 于 2017-9-9 00:06 编辑
  1. import requests
  2. from bs4 import BeautifulSoup

  3. doc = requests.get('http://www.tfls.cn/html/students/union/').content
  4. soup = BeautifulSoup(doc,'html.parser')
  5. for link in soup.find_all(valign="middle"):
  6.     for link1 in link.find_all('a'):
  7.         print('http://www.tfls.cn' + link1.get('href'))
复制代码


http://cuiqingcai.com/1319.html
先用keyword 参数筛选一遍。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-9 07:42:07 | 显示全部楼层

  1. import requests, re
  2. from bs4 import BeautifulSoup

  3. doc = requests.get('http://www.tfls.cn/html/students/union/').content
  4. soup = BeautifulSoup(doc, 'html.parser')
  5. for link in soup.find_all('a', href = re.compile('/html/students/union/[0-9]{4}.html')):
  6.     url = 'http://www.tfls.cn' + link.get('href')
  7.     print(url)
复制代码

效率更高哎
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 03:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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