【发布时间】:2016-08-08 00:56:10
【问题描述】:
A.我的目标: 使用 Python 从 Excel 电子表格中提取唯一的 OCPO ID,并使用这些 ID 在网络上抓取相应的公司名称和 NIN ID。 (注意:NIN 和 OCPO ID 对一家公司都是唯一的。
B.细节: 一世。使用 openpyxl 从 Excel 电子表格中提取 OCPO ID。 ii.在企业注册中心 (https://focus.kontur.ru/) 中逐一搜索 OCPO ID,并使用 BeautifulSoup4 查找相应的公司名称和公司 ID (NIN)。
示例:搜索 OCPO ID“00044428”会产生匹配的公司名称 ПАО "НК "РОСНЕФТЬ" 和对应的 NIN ID "7706107510"。
- 在 Excel 中保存公司名称和 NIN ID 的列表。
C.我的进步: 一世。我能够将 OCPO ID 列表从 Excel 提取到 Python。
# Pull the Packages
import openpyxl
import requests
import sys
from bs4 import BeautifulSoup
# Pull OCPO from the Spreadsheet
wb = openpyxl.load_workbook(r"C:\Users\ksong\Desktop\book1.xlsx")
sheet = wb.active
sheet.columns[0]
for cellobjc in sheet.columns[0]:
print(cellobjc.value)
二。我可以搜索 OCPO ID 并让 Python 抓取匹配的公司名称和相应的公司 NIN ID。
# Part 1a: Pull the Website
r = requests.get("https://focus.kontur.ru/search?query=" + "00044428")
r.encoding = "UTF-8"
# Part 1b: Pull the Content
c = r.content
soup = BeautifulSoup(c, "html.parser", from_encoding="UTF-8")
# Part 2a: Pull Company name
name = soup.find("a", attrs={'class':"js-subject-link"})
name_box = name.text.strip()
print(name_box)
D.帮助
我。您如何编写代码,以便将每个 OCPO ID 作为循环单独搜索,这样我就不会获得 OCPO ID 列表,而是获得搜索结果列表?换句话说,每个OCPO都被搜索并匹配到对应的Company Name和NIN ID。这个循环必须以######## ("https://focus.kontur.ru/search?query=" + "########") 的形式提供。
二。另外,我应该为 Python 使用什么代码将所有搜索结果保存在 Excel 电子表格中?
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup openpyxl