Сканируйте данные из таблицы html в python - PullRequest
1 голос
/ 28 мая 2020

Я новичок в сканировании веб-страниц, и мне нужна помощь в получении значений из таблицы. У меня есть все обязательные поля (МЕСТО, ДАТА, СВОДКА, СРОК). Я хочу, чтобы в сводке был URL-адрес другой страницы. Я хочу, чтобы этот URL-адрес был добавлен вместе с другими полями, такими как (LOCATION, DATE, SUMMARY, DEADLINE, URL )

Это веб-сайт

Пока это мой код. Но не работает

import requests as rq
from bs4 import BeautifulSoup
import pandas as pd

url = 'https://www.tendersinfo.com/global-information-technology-tenders-{}.php'

amount_of_pages = 2 #5194 
rows = []

for i in range(1,amount_of_pages):
    response = rq.get(url.format(i))


    if response.status_code == 200:
        soup = BeautifulSoup(response.text,'html.parser')
        table = soup.find('table',{'id':'datatable'})

        headers = []

        for th in table.find("tr").find_all("th"):
           headers.append(th.text.strip())

        for tr in table.find_all("tr")[1:]:
            cells = []
            tds = tr.find_all("td")

            if len(tds) == 0:
                ths = tr.find_all("th")

                for th in ths:
                    cells.append(th.text.strip())
            else:
                for td in tds:
                    cells.append(td.text.strip())
                    cells.append('https://www.tendersinfo.com/' + td.find('a')['href'])

            rows.append(cells)   

Ответы [ 3 ]

2 голосов
/ 28 мая 2020

Вот вы go, я просто перекодировал большую часть.

import requests as rq
from bs4 import BeautifulSoup
import pandas as pd

location = []
posted_date = []
summary = []
deadline = []

url = 'https://www.tendersinfo.com/global-information-technology-tenders-{}.php'

amount_of_pages = 10 # Max is 5194 currently
rows = []

for i in range(1,amount_of_pages):
    response = rq.get(url.format(i))
    if response.status_code == 200:
        soup = BeautifulSoup(response.text,'html.parser')
        table = soup.find('table',{'id':'datatable'})
        headers = []
        for th in table.find("tr").find_all("th"):
           headers.append(th.text.strip())
        for tr in table.find_all("tr")[1:]:
            cells = []
            tds = tr.find_all("td")
            if len(tds) == 0:
                ths = tr.find_all("th")
                for th in ths:
                    cells.append(th.text.strip())
            else:
                for td in tds:
                    cells.append(td.text.strip())
            rows.append(cells)

pd.DataFrame(rows, columns=headers).to_csv(r"C:\Users\HP\Desktop\Web Scraping (RFP's)\RFP_SCRAPED_DATA.csv", index=False)
1 голос
/ 28 мая 2020

Вы легко получаете таблицу, используя pd.read_html, и сохраняете эти данные в файл csv, используя df.to_csv().

import pandas as pd

url = "https://www.tendersinfo.com/ajax_all_new_search.php?country=information-technology&increment=1&%20select=500&%20total=259655&%20search_id=19906&%20order=id&%20imagevalue=1"

df = pd.read_html(url)[0]

df.to_csv("RFP_SCRAPED_DATA.csv", index=False)
1 голос
/ 28 мая 2020

Поскольку вы используете pandas, почему бы не использовать read_ html, который возвращает извлеченные таблицы в виде списка DataFrames.

>>> tables = pd.read_html("https://www.tendersinfo.com/global-information-technology-tenders.php")

>>> tables[1]

  LOCATION         DATE                                        SUMMARY     DEADLINE
0    India  21-May-2020  Liquid Crystal Display Lcd Panel Or Monitors.  01-Jun-2020
1    India  21-May-2020                          Random Access Memory.  01-Jun-2020
2    India  21-May-2020         Supply Of Analog Transceiver-handheld.  01-Jun-2020
3    India  21-May-2020                   Supply Of Computer Printers.  01-Jun-2020
4    India  21-May-2020                                 All In One Pc.  01-Jun-2020
...