您的位置:首页 > 编程语言 > Python开发

python简单爬虫 及 beautifulSoup简单用法

2017-02-14 19:28 603 查看
主要是在这里看到的:
http://www.jb51.net/article/65287.htm 
爬取电影天堂的磁力链接

#-*- coding: utf-8 -*-
import urllib.request
from bs4 import BeautifulSoup
import re

url='http://www.dytt8.net/index.htm'
res=urllib.request.urlopen(url).read()
html=res.decode('gbk')
#print(html)

soup = BeautifulSoup(html,"html.parser")

res = soup.find_all(href=re.compile('/html/gndy/dyzz/2017'))

for each in res:

each='http://www.dytt8.net/'+each['href']
res2=urllib.request.urlopen(each)
html2=res2.read().decode('gbk')
soup2 = BeautifulSoup(html2,"html.parser")
data = soup2.find_all(href=re.compile('ftp://'))
print(data[0]['href'])


创建 beautifulsoup 对象

soup = BeautifulSoup(html)

find_all() 用法 返回的是一个列表

A.name参数 = name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉

1.传字符串

2.传正则表达式

3.传列表

4.传True (没用过)

5.传方法(没用过)

B.keyword参数 = 搜索时会把该参数当作指定名字tag的属性来搜索

C.text参数返回的不是tag

soup.find_all(text="Elsie")
# [u'Elsie']

soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u'Elsie', u'Lacie', u'Tillie']

soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: