方法
import requests
from bs4 import BeautifulSoup
from fake_useragent import FakeUserAgent
hd = {'User-Agent': FakeUserAgent().random}
r = requests.get('https://www.meishij.net/fenlei/zaocan/', headers=hd)
# r.status_code # 查看状态
# r.encoding # 编码 ISO-8859-1
r.encoding = 'UTF-8' # 转换编码格式
soup = BeautifulSoup(r.text, 'html.parser') # r.content
"""
"""
# 指定标签
alst = soup.find_all('table')
for i in alst:
print(i.a.get('title'), i.span.string, '\t', i.p.string)
# 指定样式 class_
info_all = soup.find_all('div', class_='list_s2_item')
for i in info_all:
info1 = i.find('a', class_='list_s2_item_info').strong.string
info2 = i.find('a', class_='list_s2_item_info').span.string
# 指定元素
address = soup.select(
'body > div.comm-content-box.clearfloat > div > div.comm-content-left.clearfloat > div > div.xq_toggle > div:nth-child(2) > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(2)')
print(address[0].string)
"""
获取内容语法
data.get_text() # 获取data中文本内容
p.string # <p>张三<p>
a.get('title') # <a>title='张三'</a>
.text # 获取div中的内容
"""
"""
# find('p') 获取第一个p标签
# find_all('p') 获取所有标签 得到的是一个列表
title = a.p.a.get_text() # 获取数据a中 第一个p标签的第一个a标签的内容
author = a.find_all('p')[1].find('a').string # 获取数据a中 所有p标签的第二个p标签中所有a标签中的第一个内容
dynasty = a.find_all('p')[1].find_all('a')[1].string # 获取数据a中 所有p标签的第二个p标签中所有a标签中的第二个内容
content = a.find('div', style='display:none;').text
"""
借用网站功能查IP地址:
import requests
from bs4 import BeautifulSoup
from fake_useragent import FakeUserAgent
def get_address_by_ip(ip):
hd = {'User-Agent': FakeUserAgent().random}
url = 'https://ip.hao86.com/' + ip + '/'
r = requests.get(url, headers=hd)
r.encoding = 'UTF-8'
soup = BeautifulSoup(r.text, 'html.parser')
address = soup.select(
'body > div.comm-content-box.clearfloat > div > div.comm-content-left.clearfloat > div > div.xq_toggle > div:nth-child(2) > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(2)')
return address[0].string
IP = input('输入你要查询的IP:') # 221.218.142.209
address = get_address_by_ip(IP)
print(address)
保存图片:
import requests
import os
'''
爬取指定url图片
可能出现的问题:图片地址不合法,缺少http
http://upload.techweb.com.cn/s/310/2018/0410/1523330175838.jpg
'''
url4 = 'http://img.netbian.com/file/2022/1127/small1208244iXg61669522104.jpg'
theUrl = url4 # 指定要爬取的url
# 爬取到的图片存在电脑那个磁盘位置
root = "d://pythonZoneHH//pic2HH//"
path = root + theUrl.split("/")[-1] # 这句话可以保证原来图片叫什么名字,爬下来时候还叫什么名字
print(path)
try:
if not os.path.exists(root): # 判断磁盘制定文件夹是否存在,
os.makedirs(root) # 如果不存在就创建文件夹
r = requests.get(theUrl)
print("文件大小", len(r.content) / 1024, "kb")
with open(path, "wb") as f:
print("正在保存文件...")
f.write(r.content) # 向文件中写入二进制内容
print("文件保存成功")
except Exception as e:
print("爬取失败", e)
页面中不存在的数据爬取:
右键检查,从网络的请求中的找到对应数据,在标头中复制出URL
import json
from bs4 import BeautifulSoup
import requests
from fake_useragent import FakeUserAgent
re_url = 'https://www.thecfa.cn/index.html'
url = 'https://www.thecfa.cn/nvzgjd/dataApi/text/v1?contId=30235&next=-1'
hd = {'User-Agent': FakeUserAgent().random, 'Referer': re_url} # Referer:re_url 设置引用页
r = requests.get(url, headers=hd)
r.encoding = 'UTF-8'
# r.text 获取到的是一个字符串 {"text":"<h3> 国际足联排名..."} 类型:<class:'str'>
dic = json.loads(r.text) # 字符串转json数据 用 json.loads(内容)
html_doc = dic.get('text') # 通过 .get('键') 取出值即可
soup = BeautifulSoup(html_doc, 'html.parser')
info_all = soup.find_all('li')
for i in info_all:
# print(i, '....')
date = i.find('div', class_='date').get_text()
img_url = i.find_all('div', class_='country')[0].p.img.get('src')
score = i.find('div', class_='score').get_text()
img_url2 = i.find_all('div', class_='country')[1].p.img.get('src')
print(date, '\t', img_url, '\n', score, '\t', img_url2)