鍍金池/ 問答/Python  C  Office/ 如何使用beautiful soup爬取如下網(wǎng)頁源碼中的電影名稱及鏈接

如何使用beautiful soup爬取如下網(wǎng)頁源碼中的電影名稱及鏈接

使用python3 bs4爬取電影天堂的最新電影http://www.dytt8.net/
可是爬出來都是網(wǎng)頁數(shù)據(jù),很亂,可以使用soup.findAll直接找到鏈接的標(biāo)簽進行提取<a herf=。。。嗎?,我的代碼如下:
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://www.dytt8.net/')
bsObj = BeautifulSoup(html,'html.parser')
a = bsObj.findAll('div',{'class':'co_content8'})
list1 = []
for i in a:

j = i.findAll('a')
print(type(j))
print('###')
print(list1.append(str(j)))

print('list1 is :',list1)
print(type(list1))
print(len(list1))
for n in list1:

print(n.split(','))

網(wǎng)頁部分源碼如下:
圖片描述

回答
編輯回答
別傷我
可以使用soup.findAll直接找到鏈接的標(biāo)簽進行提取<a herf=。。。嗎?
可以使用的 ,簡單寫個按href里有html提取的
# -*- coding: utf-8 -*-

import urllib.request,re
from bs4 import BeautifulSoup
html = urllib.request.urlopen('http://www.dytt8.net/')
bsObj = BeautifulSoup(html,'html.parser')

bsObj1 = bsObj.find_all('a',href=re.compile('/html'))
for i in bsObj1:
    print (i['href'],i.string)

2018年1月1日 12:20
編輯回答
撿肥皂

先定位到div下的ul,再用findall提取出ul下每條<a href....>
然后在對每條<a href.....>進行href屬性的提取

可以看下這篇文章,講BeautifulySoup使用的,希望有幫助

試下代碼可行不

urls = []
hrefs  = bsObj.find('div',class_="co_area2").ul.findall('a')
for each in hrefs:
    urls.append['協(xié)議頭'+each['href']]
print(urls)    
    
2017年6月26日 01:42
編輯回答
夢一場

推薦使用CSS選擇或者xpath、pyjquery
傳統(tǒng)都用的正則,findall也可以,不過效果不好,

2018年5月2日 19:03