我想从一个包含许多医生和医院名字的网站中提取数据我想做一些评估所以我决定使用搜索栏但不幸的是似乎无法得到我想要的结果!
我该怎么做?
from bs4 import BeautifulSoup
import requests
import urllib.request
types_of_doctor = ['dermatologist', 'gynecologist', 'paediatric-surgeon', 'cardiologist', 'diabetologists', 'eye-specialist']
def search():
for query in types_of_doctor:
# Constracting http query
url = 'http://health.hamariweb.com/doctors/' + query
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
Doctors_name = soup.findAll('a', {"class" : "NormalText"})
for doctors in Doctors_name:
print(doctors.text)
links = soup.select('a')
header = types_of_doctor
filename = 'AllNames.csv'
f = open(filename, 'w')
for head in header:
f.write(head+'\t')
for doctors in Doctors_name:
print(doctors.text)
f.write(doctors.text)
search()
请您参考如下方法:
你需要移动你的
filename = 'AllNames.csv'
f = open(filename, 'w')
在循环之外。否则,您正在为每个查询初始化和覆盖文件。
def search():
filename = 'AllNames.csv'
f = open(filename, 'w')
for query in types_of_doctor:
