簡體   English   中英

Web 使用 python 抓取 web 頁面的控制台

[英]Web scraping the console of a web page using python

所以我有這段代碼 web 抓取某個站點並打印出它在該特定網頁上找到的內容。 我對此很陌生,所以我的問題是如何僅從控制台收集數據,就像圖片中看到的那樣。

檢查控制台

這是到目前為止的代碼,感謝您的幫助

導入請求

url = '網址在這里'

r = requests.get(url)

打印(r.text)

下面是一些收集output的方法:

  1. 如果數據非常小且格式正確,例如每個 URL 僅 1 行,您可以從控制台打印中復制 output。

  2. 如果數據很大,我假設這是您的情況,您可以將 output 寫入文件。

    import requests
    
    url = 'url goes here'

    r = requests.get(url)
    
    print(r.text)  
    
    with open('/path/to/file.txt', 'w', encoding='utf-8') as f:
    
        f.write('r.text')
  1. 如果您有數千個 URL,並且需要寫入數千個文件,只需為每個 url 添加一個 for 循環並將 output 寫入不同的文件。

above example are using a txt file, you can also write the output into a.xml file or.html file, any format that is more convenient to re-use for you, like docx, excel, csv, json, etc.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM