我有以下湯:

下一個...從中我想提取href,“some_url”,我想提取href,“some_url”以及此頁面上列出的頁面的整個串列:
https://www.catholic-hierarchy.org/diocese/laa.html
注意:有很多子頁面的鏈接:我需要決議。目前:獲取所有資料:-dioceses -Urls -description -contact-data -etc。等。
以下是以異步方式獲取該資訊的一種方式(應該在 Colab 筆記本上作業)。我從網站的不同部分獲得了教區網址(結構化視圖 - 世界地區)。我希望那里的教區數量與信件串列中的數量相匹配。
from httpx import Client, AsyncClient, Limits
from bs4 import BeautifulSoup as bs
import pandas as pd
import re
from datetime import datetime
import asyncio
import nest_asyncio
nest_asyncio.apply()
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
big_df_list = []
def all_dioceses():
dioceses = []
root_links = [f'https://www.catholic-hierarchy.org/diocese/qview{x}.html' for x in range(1, 8)]
with Client(headers=headers, timeout=60.0, follow_redirects=True) as client:
for x in root_links:
r = client.get(x)
soup = bs(r.text)
soup.select_one('ul#menu2').decompose()
for link in soup.select('ul > li > a'):
dioceses.append('https://www.catholic-hierarchy.org/diocese/' link.get('href'))
return dioceses
# print(all_dioceses())
async def get_diocese_info(url):
async with AsyncClient(headers=headers, timeout=60.0, follow_redirects=True) as client:
try:
r = await client.get(url)
soup = bs(r.text)
d_name = soup.select_one('h1[align="center"]').get_text(strip=True)
info_table = soup.select_one('div[id="d1"] > table')
d_bishops = ' | '.join([x.get_text(strip=True) for x in info_table.select('td')[0].select('li')])
d_extra_info = ' | '.join([x.get_text(strip=True) for x in info_table.select('td')[1].select('li')])
big_df_list.append((d_name, d_bishops, d_extra_info, url))
print('done', d_name)
except Exception as e:
print(url, e)
async def scrape_dioceses():
start_time = datetime.now()
tasks = asyncio.Queue()
for x in all_dioceses():
tasks.put_nowait(get_diocese_info(x))
async def worker():
while not tasks.empty():
await tasks.get_nowait()
await asyncio.gather(*[worker() for _ in range(100)])
end_time = datetime.now()
duration = end_time - start_time
print('diocese scraping took', duration)
asyncio.run(scrape_dioceses())
df = pd.DataFrame(big_df_list, columns = ['Name', 'Bishops', 'Info', 'Url'])
print(df)
這將導致以下結果:
done Eparchy of Mississauga (Syro-Malabar)
done Eparchy of Mar Addai of Toronto (Chaldean)
done Eparchy of Saint-Sauveur de Montr?al (Melkite Greek)
done Diocese of Calgary
done Archdiocese of Winnipeg
[...]
diocese scraping took 0:03:02.366096
Name Bishops Info Url
0 Eparchy of Mississauga (Syro-Malabar) JoseKalluvelil, Bishop Type of Jurisdiction: Eparchy | Elevated:22 December2018 | Immediately Subject to the Holy See | Syro-Malabar Catholic Church of the Chaldean Tradition | Country:Canada | Mailing Address: Syro-Malabar Apostolic Exarchate, 6630 Turner Valley Rd., Mississauga, ON L5V 2P1, Canada | Telephone: (905)858-8200 | Fax: 858-8208 https://www.catholic-hierarchy.org/diocese/dmism.html
1 Eparchy of Mar Addai of Toronto (Chaldean) Robert SaeedJarjis, Bishop | Bawai (Ashur)Soro, Bishop Emeritus Type of Jurisdiction: Eparchy | Erected:10 June2011 | Immediately Subject to the Holy See | Chaldean Catholic Church of the Chaldean Tradition | Country:Canada | Conference Region:Ontario | Mailing Address: 2 High Meadow Place, Toronto, ON M9L 2Z5, Canada | Telephone: (416)746-5816 | Fax: 746-5850 https://www.catholic-hierarchy.org/diocese/dtoch.html
2 Eparchy of Saint-Sauveur de Montr?al (Melkite Greek) MiladJawish, B.S., Bishop Type of Jurisdiction: Eparchy | Elevated:1 September1984 | Immediately Subject to the Holy See | Melkite Greek Catholic Church of the Byzantine Tradition | Country:Canada | Conference Region:Quebec | Web Site:http://www.melkite.com/ | Mailing Address: 10025 boul. de l'Arcadie, Montreal, QC H4N 2S1, Canada | Telephone: (514)272.6430 | Fax: 202.1274 https://www.catholic-hierarchy.org/diocese/dmome.html
注意- 對我來說,在 collab 上運行它是不可能的 - 如何簡化它以便在 collab 中運行此代碼!?
好吧 - 我得到了錯誤 - 我在協作中運行它時得到了這個:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-64bb145c85bf> in <module>
----> 1 from httpx import Client, AsyncClient, Limits
2 from bs4 import BeautifulSoup as bs
3 import pandas as pd
4 import re
5 from datetime import datetime
ModuleNotFoundError: No module named 'httpx'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
注意- 對我來說,在 collab 上運行它是不可能的 - 如何簡化它以便在 collab 中運行此代碼!?
Mauro Martins 提到要運行這個——但是等等;我不是 collab 的專業用戶。所以問題是:如何簡化這個我可以在 colab 上運行它 - 在一個普通的協作帳戶上
!pip install httpx nest_asyncio
Try running this code before your script.
非常感謝您的快速回復。驚人的。我理解你的方法:但我需要一個 colab 的專業帳戶 - 注意:我沒有這個。所以問題是:我可以簡化腳本,以便它可以在一般協作帳戶上運行 - 沒有任何問題
非常感謝 - 親愛的 Mauro Martins Junior - 它有幫助 - 此代碼有幫助。:
!pip install httpx nest_asyncio
筆記:

更新:感謝 Mauro Martin,我學會了將插件更新到 colab:
如何在 Google 的 Colab 中安裝 Python 包?
如何在 Google 的 Colab 中安裝 Python 包?
在一個專案中,我有兩個不同的包,我如何使用 setup.py 在谷歌的 Colab 中安裝這兩個包,以便我可以匯入包?
看答案:
你可以使用 !setup.py install 來做到這一點。Colab 就像一個 Jupyter 筆記本。因此,我們可以使用 ! 操作員在此處安裝 Colab 中的任何軟體包。什么 !實際上,它告訴筆記本單元這行不是 Python 代碼,而是命令列腳本。因此,要在 Colab 中運行任何命令列腳本,只需添加一個 ! 行前。例如:!pip install tensorflow。這會將該行(此處為 pip install tensorflow)視為命令提示行,而不是一些 Python 代碼。但是,如果您在不添加 ! 在該行之前,它會拋出一個錯誤,說“無效的語法”。但請記住,在執行此操作之前,您必須將 setup.py 檔案上傳到您的驅動器(最好與筆記本所在的檔案夾相同)。
還有conda 環境:
google colab [google-colaboratory] ??中的 conda 環境 google colab [google-colaboratory] ??中的 conda 環境
我正在嘗試在 google colab notebook 中創建一個 conda 環境。我使用以下命令成功安裝了 conda
google colab [google-colaboratory] ??中的 conda 環境 google colab [google-colaboratory] ??中的 conda 環境 我正在嘗試在 google colab notebook 中創建一個 conda 環境。我使用以下命令成功安裝了 conda
我正在嘗試在 google colab notebook 中創建一個 conda 環境。我使用以下命令成功安裝了 conda
!wget -c https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
!chmod x Anaconda3-5.1.0-Linux-x86_64.sh
!bash ./Anaconda3-5.1.0-Linux-x86_64.sh -b -f -p /usr/local
系統使用的默認 python 現在是 Python 3.6.4 :: Anaconda, Inc.
我正在嘗試通過 conda env create -f environment.yml 在 conda 中創建一個環境
每個軟體包都已成功安裝,但現在的問題是我無法激活此環境。我試過源激活 myenv。但它也沒有奏效。
在 conda env list 命令之后,我得到了兩個環境
base * /usr/local
myenv /usr/local/envs/myenv
uj5u.com熱心網友回復:
似乎問題在于缺少一些庫。我嘗試在 colabs 上運行,運行下面的命令后,它運行良好。
!pip install httpx nest_asyncio
嘗試在您的腳本之前運行此代碼。
請注意,感嘆號是命令的一部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525830.html
上一篇:R決議字串以提取、計算和替換數字
