找不到元素
先打印html或soup看一下 是不是和f12展示的一样的结构
有明显id的可能有record /record 标签
eg.天/津/市/卫/健/委健/康知识
看看是不是post请求
eg.天/津/市/卫/健/委健/康知识
可能返回的是json文件(如下图)直接解析json
res = requests.get(self.url)
html = res.json()
url没有显示换页
会有iframe 找到它的src属性 用这个url值
中文乱码
.encode(“ISO_8859_1”).decode(“utf-8”)
请求页面的时候加上一句编码的话
res = requests.get(self.url,headers = self.header,verify=False)
res.encoding = res.apparent_encoding
要下载文档的话
from urllib.request import urlretrieve
file=os.path.splitext(a[‘href’])
filename,type=file
file_name = a.text + type
urlretrieve(new_url, file_name)
a.text里面不能有特殊字符
不然报错:
tfp = open(filename, ‘wb’)
FileNotFoundError: [Errno 2] No such file or directory: ‘饮用水水源保护区标志技术要求(HJ/T 433-2008).pdf’
driver语句
self.driver = webdriver.Chrome()
self.driver.get(self.url)
time.sleep(random.uniform(3, 5))
self.list_parase()
点击下一页
self.driver.find_element_by_xpath(“XXX”).click()
time.sleep(random.uniform(2, 3))
self.list_parase()
时间戳转日期格式
timeStamp = li[“date”]
timeArray = time.localtime(timeStamp)
date = time.strftime("%Y-%m-%d", timeArray)
print(date)