簡體   English   中英

python、網頁抓取和寫入文件

[英]python, webscraping, and writing a file

我在這個程序中使用了 3 個模塊,我不知道我想做什么甚至可能! 所以我想從 twitter 上抓取一些數據並使用 python 將其寫入文本文件中,有人可以指導我並告訴我為什么我的代碼沒有寫入報廢的數據嗎?

import urllib
import urllib.request
from os import path
from bs4 import BeautifulSoup

# here I define the url, I request the page, create my soup
theurl = "https://twitter.com/realDonaldTrump"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")

def create_file(dest):
    """
    Creates a file for the user to write data in!
    :param dest:
    :return:
    """
    ## FileName == Month_Day_Year
    name = 'Data Scraped.txt'

    if not(path.isfile(dest +name)):
        f = open(dest + name, "w")
        f.write(soup.title.text)
        f.close()

if __name__ == '__main__':
    destination = 'C:\\Users\\edwin\\' \
              'Desktop\\WebScrappin\\'
    create_file(destination)
    print("Your file has been created!!")

您只是編寫您收到的文檔的標題。

        f.write(soup.title.text)

您應該從他們的RESTful API 中收集數據或使用像Twython這樣的庫,而不是抓取(這違反了他們的 ToS

暫無
暫無

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

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