current position:Home>How to use the BeautifulSoup method to extract the tags in the webpage code, and put the extracted data into the excel file in turn
How to use the BeautifulSoup method to extract the tags in the webpage code, and put the extracted data into the excel file in turn
2022-08-06 16:03:51【CSDN Q&A】
如何用BeautifulSoupMethods to extract tags inside the web page source code,将提取出的数据,依次放入excel文件中
问题相关代码
import xlwtfrom bs4 import BeautifulSoupif __name__=="__main__": wookbook = xlwt.Workbook() #创建工作簿 sheet1 = wookbook.add_sheet('Sheet_one',cell_overwrite_ok=True) #创建sheet,名字为Sheet_one headlist = ['序号','列名','英文名'] #表头数据 row = 0 col = 0 # 写入表头数据 for head in headlist: sheet1.write(row, col, head) col = col + 1html = """<thead><tr><th class="bs-checkbox " style="width: 36px; " data-field="ck" tabindex="0"><div class="th-inner "><input name="btSelectAll" type="checkbox"></div><div class="fht-cell"></div></th><th style="" data-field="isInside" tabindex="0"><div class="th-inner ">Belong to the mobile inside or outside the network number</div><div class="fht-cell"></div></th><th style="" data-field="businessCategory" tabindex="0"><div class="th-inner ">业务类别(Inbound access)</div><div class="fht-cell"></div></th>"""soup = BeautifulSoup(html, 'lxml')tr_list = soup.find_all('tr')[1:]for th in soup.select('th'): print(th['data-field'])headlist=th['data-field']row = 1 # Starting form the second line of writing datafor c, top in enumerate(headlist): sheet1.write(row, 2, top) # rou代表列,col代表行,top.text写入值row += 1#wookbook.save(r'D:\test.xls')
运行结果及报错内容
The data you need is the output out,但是我导入excelFile failed to import
我的解答思路和尝试过的方法
I tried all kinds of ideas to take out the data,Have put them into the list,But they are each data into a list,Then the import,Only import the first,Can't import success in turn.
Used to convert data into the list,然后再导入:
import xlwtfrom bs4 import BeautifulSoupif __name__=="__main__": wookbook = xlwt.Workbook() #创建工作簿 sheet1 = wookbook.add_sheet('Sheet_one',cell_overwrite_ok=True) #创建sheet,名字为Sheet_one headlist = ['序号','列名','英文名'] #表头数据 row = 0 col = 0 # 写入表头数据 for head in headlist: sheet1.write(row, col, head) col = col + 1html = """<thead><tr><th class="bs-checkbox " style="width: 36px; " data-field="ck" tabindex="0"><div class="th-inner "><input name="btSelectAll" type="checkbox"></div><div class="fht-cell"></div></th><th style="" data-field="isInside" tabindex="0"><div class="th-inner ">Belong to the mobile inside or outside the network number</div><div class="fht-cell"></div></th><th style="" data-field="businessCategory" tabindex="0"><div class="th-inner ">业务类别(Inbound access)</div><div class="fht-cell"></div></th>"""soup = BeautifulSoup(html, 'lxml')for th in soup.select('th'): headlist=th['data-field'] A = headlist.split() print(A)row = 1 # Starting form the second line of writing datafor c, top in enumerate(A): sheet1.write(row, 2, top) # rou代表列,col代表行,top.text写入值row += 1#wookbook.save(r'D:\test.xls')
Results entered a list,And I feel the three list is a list of..
我想要达到的结果
copyright notice
author[CSDN Q&A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/218/202207310003021564.html
The sidebar is recommended
- C language programming Gao Fushuai problem, error
- How to get data from other tables in the drop-down list of ElementUI
- C language, find the average, when outputting the result, use %d to output an integer, and use %f, %lf to output 0.0000
- Front-end and back-end data transfer -- about json
- Why can't the file name of VScode contain spaces?
- Looking for an internship in java, a bright resume
- Does the network layer have flow and congestion control?If so, by what?
- typescript import and export
- Find the length of the longest ascending subsequence sum, I want to ask a beginner to understand it, just getting started with C language
- Selected Retransmission (SR) related issues
guess what you like
Code to write good, why the Xcode couldn't run the results?
Beginners request you correct the mistake
ASP.NET winfrom form has changed the code but the form has no effect
Why can webscoket be used without a defined method? What is the principle?
Questions about close the terminal output
I set the global mapping table prefix in yml, but the database does not recognize it
About usb_cam ghosting problem
Centos stream 9 desktop applications all disappeared, how to do?
Chinese garbled characters in pycharm console
Visual Studio Express 2015 cannot be downloaded
Random recommended
- Solve what is wrong with my program
- The zblog program wants to call the subcategories of the current category on the article page
- Xpath parsing out list is empty, no tbody annotations
- How to batch retrieve the HMDB name of a column in excel on the HMDB website and copy the Chinese name to the excel
- In PPT form type judgment
- Python installation torch
- VC++ operates Excel to display sheet3 page
- VC++ operates Excel to display Sheet3 page directly
- Does \W+ match everything in Python regular expressions?Why do I need to write \W+ twice here?
- Does QT have a good way to determine whether the current coordinates are within the screen (supporting multiple screens).
- The values of b[i] are -1 2 4 3 5 respectively. This is what I use to find the maximum interval sum of consecutive numbers in the array. Shouldn't it be 14? Why do I calculate 25, and they all add up25
- I can't run the program that adds other people's strings
- Array of JAVA objects..
- Error in C language programming
- When TCGAbiolinks processes data, the length [2] of the 'names' attribute must be the same as the length [0] of the vector
- What is the domain-specific knowledge?
- What algorithm do you use for video clustering?What can be divided into?
- What is the similarity method using shot keyframes?What are the characteristics?What's the question?
- How are the lenses clustered?
- What is the method to calculate the similarity of pairs of frames in a shot?What are the characteristics?
- How to measure the similarity of the lens?
- How nothing the same analysis scenarios on the vision?
- C++ real source file not compiled
- Password question no output question
- # After Spring learns the basic operations, how to practice?How to study in depth?
- What is Video Event Detection?In which fields is it applied?
- Password corresponding conversion related issues
- A c language program fill in the blanks!!!!
- Object-oriented multiple-choice questions Can you give me an explanation?
- Webstorm opens and browses md files with garbled characters?
- PHP+HTML+MySQL realizes login error
- What's the matter with cv2 image to pixel conversion
- Can anyone recommend a file storage system within a local area network?
- About using Mybatis to precompile errors
- c++ for loop execution speed problem
- paddle framework, download dataset fails with garbled characters
- Use the esp8266 module to connect to the Tlink platform, the wifi connection is normal, but the Tlink platform cannot be connected
- Promise resolve execution order?
- Why should the code prompt of thinkphp6 be copied in thinkphp5, why not bring it directly in 6?
- vue2 typescript style type error?