繁体   English   中英

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

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

我想从一个包含许多医生和医院名称的网站中提取数据我想做一些评估所以我决定使用搜索栏但不幸的是似乎无法得到我想要的结果!

我怎样才能做到这一点?

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:

从网站中提取信息的技术是网络抓取 该技术主要侧重于将网络上的非结构化数据(HTML 格式)转换为结构化数据(数据库或电子表格)。

您可以通过多种方式执行网页抓取。 其中之一是通过使用BeautifulSoup 的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

根据您的需要调整它。

暂无
暂无

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

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