Python爬虫

import requests

response = requests.get("http://www.baidu.com")

print(response.text) #str 有乱码

response.encoding = 'utf8'
print(response.text)

print(response.content) #二进制
print(response.content.decode()) #将二进制解码
import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup('<html>data</html>','lxml')

print(soup)
import requests
from bs4 import BeautifulSoup

'''
find(self,name=None,attrs={},recursive=True,text=None,**kwargs)
name:标签名<a>
attrs:属性字典class
recursive:是否递归循环查找
text:根据文本内容查找
返回:查找到第一个元素对象
find_all

find(id='link1')
find(attrs={'id':'link1'})

find(text='123')

a.name 标签名
a.attts 属性
a.text 文本内容
'''
import requests
from bs4 import BeautifulSoup

response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')

home_page = response.content.decode()

soup = BeautifulSoup(home_page,'lxml')

script = soup.find(id='getListByCountryTypeService2true')

text = script.text
print(text)
import re

rs = re.findall('a\nbc','a\nbc')
print(rs)

rs = re.findall('a\\nbc','a\\nbc')
print(rs)

rs = re.findall('a\\\\nbc','a\\nbc')
print(rs)

rs = re.findall(r'a\nbc','a\nbc')
print(rs)

rs = re.findall(r'\d','a123')
print(rs)
import json

import requests
from bs4 import BeautifulSoup
import re
response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')

home_page = response.content.decode()

soup = BeautifulSoup(home_page,'lxml')

script = soup.find(id='getListByCountryTypeService2true')

text = script.text
#print(text)
json_str = re.findall(r'\[.+\]',text)[0]

#print(json_str)

last_day = json.loads(json_str)

print(last_day)
上一篇:深入了解 zookeeper 的 watcher 机制!


下一篇:LOJ6062「2017 山东一轮集训 Day2」Pair