我想从一个包含许多医生和医院名称的网站上提取数据,因此我想进行一些评估,因此我决定使用搜索栏,但不幸的是似乎无法获得我想要的结果!
我怎样才能做到这一点?
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: