from bs4 import BeautifulSoup import requests import openpyxl def get_html(url): try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: r='fail' return r def get_contents(ulist,rurl): soup=BeautifulSoup(rurl,'html.parser') trs=soup.find_all('tr') for tr in trs: ui=[] for td in tr: ts=td.string ts=ts.strip() if ts == '': continue ui.append(ts) ulist.append(ui) def saveList(ulist): wb=openpyxl.Workbook() ws=wb.active r=1 for line in ulist: for col in range(1,len(line)+1): ws.cell(row=r,column=col).value=line[col-1] r=r+1 wb.save('saveqiye500.xlsx') if __name__=="__main__": urli=[] url="https://www.maigoo.com/news/540732.html" rs=get_html(url) get_contents(urli,rs) saveList(urli)为什么数据无法导入到表格中
时间: 2024-04-28 10:23:53 浏览: 11
有可能是以下几个原因导致数据无法导入到表格中:
1. 程序没有正确解析网页内容:在使用 BeautifulSoup 库解析网页内容时,需要根据具体的 HTML 结构来进行解析,如果解析错误,可能会导致数据不完整或者无法解析。建议检查代码中的解析逻辑是否正确,以及是否正确识别目标数据所在的 HTML 元素。
2. 程序没有正确读取数据:在使用 openpyxl 库将数据保存至 Excel 表格时,需要注意表格的格式和结构,以及单元格的数据类型和格式。如果数据类型和格式不符,可能会导致数据无法正确导入。建议检查代码中的单元格赋值逻辑是否正确,以及对应单元格的数据类型和格式是否正确。
3. 程序没有正确保存数据:在使用 openpyxl 库将数据保存至 Excel 表格时,需要确保表格的文件名和保存路径正确,以及表格文件的权限是否允许写入。如果没有正确设置保存路径或者没有权限写入,可能会导致数据无法正确保存。建议检查代码中的保存逻辑是否正确,以及保存路径和文件权限是否正确设置。
建议逐一排查以上问题,以找出代码中的问题所在。如果仍然无法解决问题,可以提供更多代码和错误信息,以便更好地帮助您解决问题。
相关问题
from bs4 import BeautifulSoup import requests import openpyxl def get_html(url): try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: r='fail' return r def get_contents(ulist,rurl): soup=BeautifulSoup(rurl,'html.parser') trs=soup.find_all('tr') for tr in trs: ui=[] for td in tr: ts=td.string ts=ts.strip() if ts == '': continue ui.append(ts) ulist.append(ui) def saveList(ulist): wb=openpyxl.Workbook() ws=wb.active r=1 for line in ulist: for col in range(1,len(line)+1): ws.cell(row=r,column=col).value=line[col-1] r=r+1 wb.save('saveqiye500.xlsx') if __name__=="__main__": urli=[] url="https://www.maigoo.com/news/540732.html" rs=get_html(url) get_contents(urli,rs) saveList(urli)
这段代码是一个 Python 爬虫,目的是从指定网址 https://www.maigoo.com/news/540732.html 中获取企业社会责任500优完整名单,并将结果保存在 Excel 表格中。
代码中使用了 requests 库和 BeautifulSoup 库来获取网页内容和解析网页。其中,get_html() 函数用于获取网页内容,get_contents() 函数用于解析网页内容并将结果保存在一个列表中,saveList() 函数用于将列表中的数据保存在 Excel 表格中。
代码中的 if __name__=="__main__": 是 Python 中的特殊语句,表示如果该脚本被直接运行,而不是被导入到其他脚本中,那么就执行下面的语句。在这里,程序首先定义了一个空列表 urli,然后调用 get_html() 函数获取网页内容,接着将网页内容传递给 get_contents() 函数解析网页,并将解析得到的结果保存在 urli 列表中,最后将 urli 列表中的数据保存在 Excel 表格中。
不过需要注意的是,这段代码并没有考虑网页爬取的法律和道德问题,如果没有得到网站所有者的许可,对网站进行爬取可能会触犯法律,严重的甚至可能会导致刑事责任。因此,建议在进行网页爬取前,先了解相关法律法规和伦理道德规范,并遵守相关规定。
import requests from bs4 import BeautifulSoup def getHTMLText(url): try: r = requests.get(url,timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "产生异常" def jiexi(html_doc): soup = BeautifulSoup(html_doc,"html.parser") links = soup.find_all('a') for link in links: print(link.name,link["nref"],link.get.next()) if __name__ == 'main': url = "https://wallhaven.cc/toplist?page=7" print(getHTMLText(url))改进代码
这段代码可以改进的地方有:
1. 函数名jiexi不太直观,可以改为更明确的parse_links。
2. 在解析链接时,可以加上判断,如果链接是以http或https开头的,才输出链接信息,否则忽略。
3. 在主程序中,应该调用parse_links函数,而不是直接打印HTML页面内容。
改进后的代码如下:
```
import requests
from bs4 import BeautifulSoup
def get_html_text(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "产生异常"
def parse_links(html_doc):
soup = BeautifulSoup(html_doc, "html.parser")
links = soup.find_all('a')
for link in links:
if link.has_attr('href') and (link['href'].startswith('http') or link['href'].startswith('https')):
print(link.name, link['href'], link.get_text())
if __name__ == '__main__':
url = "https://wallhaven.cc/toplist?page=7"
html_text = get_html_text(url)
parse_links(html_text)
```
这样修改后,代码更加清晰易懂,功能也更加完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)