作者MOONY135 (谈无慾)
看板Python
标题Re: [问题] bs4抓取连结问题
时间Fri Aug 7 18:28:42 2015
※ 引述《nendi (淡定~)》之铭言:
: 您好,我是一个刚学一个月的新手,有些指令还不太熟悉
: 我想抓取某个网页的超连结
: 其中我要的连结都在 <ul class="no_listyle ar_grd"> 之下
: 我的程式码如下
: from bs4 import BeautifulSoup
: import requests
: res= requests.get('http: xxx')
: #实际网址太长,我用缩网址发文被说是广告,只好写xxx代替
: soup = BeautifulSoup(res.text)
: ul = soup.findAll('ul',{'class':"no_listyle ar_grd"})
: #找出来後,我想把<a href=" ">之中的超连结取出来,所以我试接着写
: for link in ul:
: print(link.find('a')['href'])
: 但只显现出了第一个连结,我希望能把所有超连结取出
: 可否哪位好心大大指导一下呢?
: 感谢
res_index = requests.get(xxxx)
soup_index = BeautifulSoup(res_index.text,"html.parser")
main_container_index = soup_index.select('.r-ent')
for link in main_container_index:
try:
Post_link = link.find('a')['href']
我每取出一笔就直接写入csv档这样 可以参考一下
或者参考
=======================================================
import requests
from bs4 import BeautifulSoup
import sys
res_index = requests.get("
https://webptt.com/cn.aspx?n=bbs/gamesale/index.html")
soup_index = BeautifulSoup(res_index.text,"html.parser") #抓每篇文章的URL联结
main_container = soup_index.find_all('a', href=True)
href = main_container[7].get('href')[19:23]
print(type(href))
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 180.177.187.24
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1438943324.A.9C0.html
※ 编辑: MOONY135 (180.177.187.24), 08/07/2015 18:32:00