|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import requests
- from bs4 import BeautifulSoup
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
- Chrome/76.0.3809.100 Safari/537.36'
- }
- # 这里是想查的CSDN的备案信息
- res = requests.get('https://icp.aizhan.com/www.csdn.net/', headers = headers)
- # 异常处理
- try:
- # 对返回结果进行解析
- soup = BeautifulSoup(res.text, 'html.parser')
- # 通过find_all()获取主办单位名称
- div = soup.find('div', attrs = {'id':'icp-table'})
- td_list = div.find_all('td')
- name_info = td_list[0].text + ":" + td_list[1].text
- print(name_info)
- # 通过selector获取备案号
- icp_no = soup.select('#icp-table > table > tr:nth-of-type(3) > td:nth-of-type(2) > span')[0].get_text()
- print(icp_no)
- except ConnectionError:
- print("连接失败")
复制代码
我想把网站上的指定信息爬下来,但是报错说div为None,但我检查了一下网页上面的确是有这个元素的,请问是什么原因呢?
- import requests
- from bs4 import BeautifulSoup
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
- Chrome/76.0.3809.100 Safari/537.36'
- "cookie":"将浏览器的cookie替换这里的文字"}
- # 这里是想查的CSDN的备案信息
- res = requests.get('https://icp.aizhan.com/www.csdn.net/', headers = headers)
- # 异常处理
- try:
- # 对返回结果进行解析
- soup = BeautifulSoup(res.text, 'html.parser')
- # 通过find_all()获取主办单位名称
- div = soup.find('div', attrs = {'id':'icp-table'})
- td_list = div.find_all('td')
- name_info = td_list[0].text + ":" + td_list[1].text
- print(name_info)
- # 通过selector获取备案号
- icp_no = soup.select('#icp-table > table > tr:nth-of-type(3) > td:nth-of-type(2) > span')[0].get_text()
- print(icp_no)
- except ConnectionError:
- print("连接失败")
复制代码
|
|