import requests from bs4 import BeautifulSoup response = requests.get(url='http://www.ifeng.com/')#网页 # response.encoding = 'utf8' response.encoding = response.apparent_encoding#编码问题 # print(response.text) soup = BeautifulSoup(response.text,features='html.parser')#(lxml)#变为对象,第二个参数为模式 target = soup.find(id = 'turnRed')#找到id obj = target.find_all('li')#id 下的所有li # print(obj) for i in obj: a = i.find('a')#找到所有a标签 if a: print(a.attrs.get('href'))#找到属性
总结:
总结一:
1、requests
response = requests.get('URL')
response.content #返回字节流
response.enconding
response.aparent_encoding
response.status_code
2 soup = beautifulsoup('<html>...)',features = 'html.parser')
v1 = soup.find('div')
soup.find(id = 'il')
soup.find('div',id='il)
v2 = soup.find_all('div') #返回列表
obj = v1
obj = v[0]
obj.text #获取文本
obj.attrs #获取属性