简体   繁体   English

如何使用python从网站的搜索栏中提取数据?

[英]How to extract data from a website's search bar using python?

I want to extract data from a website that contains the names of many doctors and hospitals I want to do some evaluations so I decided to use the search bar but unfortunately cannot seem to get my desired result!我想从一个包含许多医生和医院名称的网站中提取数据我想做一些评估所以我决定使用搜索栏但不幸的是似乎无法得到我想要的结果!

How can I do that?我怎样才能做到这一点?

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()

You need to move your你需要移动你的

    filename = 'AllNames.csv'
    f = open(filename, 'w')

outside of the loop.在循环之外。 Otherwise you are initializing and overwriting the file for each query.否则,您正在为每个查询初始化和覆盖文件。

    def search():
    filename = 'AllNames.csv'
    f = open(filename, 'w')
         for query in types_of_doctor:

The technique to extract information from websites is Web scraping .从网站中提取信息的技术是网络抓取 This technique mostly focuses on the transformation of unstructured data (HTML format) on the web into structured data (database or spreadsheet).该技术主要侧重于将网络上的非结构化数据(HTML 格式)转换为结构化数据(数据库或电子表格)。

You can perform web scraping in various ways.您可以通过多种方式执行网页抓取。 One of these is to use Python using BeautifulSoup which assists this task.其中之一是通过使用BeautifulSoup 的Python 来帮助完成这项任务。

Please, read the articles below:请阅读以下文章:

https://www.analyticsvidhya.com/blog/2015/10/beginner-guide-web-scraping-beautiful-soup-python/ https://www.analyticsvidhya.com/blog/2015/10/beginner-guide-web-scraping-beautiful-soup-python/

https://medium.freecodecamp.org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe https://medium.freecodecamp.org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe

adapting it for your needed.根据您的需要调整它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM