主要是在这里看到的:
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"]