需要python中的web抓取帮助吗

需要python中的web抓取帮助吗,python,web-scraping,Python,Web Scraping,我写的代码从每个代码(A代码、B代码、C代码等)的内部链接中删除数据 若您运行我的代码,它会刮取数据,但并不像预期的那个样,我的预期结果如下图所示 需要在cvs文件中输入以下所有列名和数据,如结果图像,即“组”“类别”“代码”“长描述”“短描述” [ 这是密码 从bs4导入美化组 导入请求 导入csv baseurl=请求。获取(“https://www.hcpcsdata.com/Codes)。文本 baseurlhcpc=https://www.hcpcsdata.com' soup=Be

我写的代码从每个代码(A代码、B代码、C代码等)的内部链接中删除数据

若您运行我的代码,它会刮取数据,但并不像预期的那个样,我的预期结果如下图所示

需要在cvs文件中输入以下所有列名和数据,如结果图像,即“组”“类别”“代码”“长描述”“短描述”

[

这是密码

从bs4导入美化组
导入请求
导入csv
baseurl=请求。获取(“https://www.hcpcsdata.com/Codes)。文本
baseurlhcpc=https://www.hcpcsdata.com'
soup=BeautifulSoup(baseurl,'lxml')
#文件=打开('hcpccode3.csv','w')
#writer=csv.writer(文件)
#writer.writerow([“hcpc代码”,“说明”])
查找所有('tr',class='clickable-row'):
hcpc_代码=table.td.a.text
#打印(hcpc_代码)
description=table.find_all('td')[2].text.strip()
打印(说明)
#writer.writerow([hcpc_代码,说明])
codelinks=soup.find_all('tr',class='clickable-row')
codelinksall=[]
对于代码链接中的项目:
对于项目中的链接。查找所有('a',href=True):
codelinksall.append(baseurlhcpc+link['href'])
打印(codelinksall)
对于codelinksall中的链接:
r=请求。获取(链接)
汤=BeautifulSoup(r.含量,'lxml')
查找所有('tr',class='clickable-row'):
代码=table.td.a.text
description1=table.find_all('td')[1].text.strip()
打印(代码、说明1)

如果我了解您期望得到的结果,下面是我是如何做到的:

import requests
from bs4 import BeautifulSoup
import csv

response = requests.get(url="https://www.hcpcsdata.com/Codes")
print(response.status_code)

soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find("div",{"class":"body-content"}).find("table",{"class":"table"}).find("tbody")
table_elements = table.find_all("tr",{"class":"clickable-row"})

elements_table = []

for i in table_elements:
  items = i.find_all("td")
  elements = []
  for i in items:
    elements.append(i.get_text().strip())
  elements_table.append(elements)

with open("table.csv","w+") as table:
  csv_writer = csv.writer(table, delimiter=",")
  for i in elements_table:
    csv_writer.writerow([*i])
它返回此CSV文件:

'A' Codes,678,"Transportation Services Including Ambulance, Medical & Surgical Supplies"
'B' Codes,50,Enteral And Parenteral Therapy
'C' Codes,367,Temporary Codes For Use with Outpatient Prospective Payment System
'E' Codes,608,Durable Medical Equipment
'G' Codes,"1,736",Procedures / Professional Services (Temporary Codes)
'H' Codes,88,Alcohol and Drug Abuse Treatment Services / Rehab Services
'J' Codes,824,"Drugs Administered Other Than Oral Method, Chemotherapy Drugs"
'K' Codes,144,Durable Medical Equipment For Medicare Administrative Contractors
'L' Codes,904,"Orthotic And Prosthetic Procedures, Devices"
'M' Codes,117,Medical Services
'P' Codes,56,Pathology And Laboratory Services
'Q' Codes,359,Miscellaneous Services (Temporary Codes)
'R' Codes,3,Diagnostic Radiology Services
'S' Codes,526,Commercial Payers (Temporary Codes)
'T' Codes,109,Established For State Medical Agencies
'U' Codes,4,Coronavirus Diagnostic Panel
'V' Codes,209,"Vision, Hearing And Speech-Language Pathology Services"
我希望我能帮忙