【问题标题】:Beginner Scraping Project: How to transform Twitter ids into usernames?初学者抓取项目:如何将 Twitter id 转换为用户名?
【发布时间】:2019-07-15 16:56:45
【问题描述】:

感谢@BittoBennichan,我已经能够build这个小python东西,它可以抓取Twitter上发布的媒体中标记的用户ID:

from bs4 import BeautifulSoup
from selenium import webdriver
import time

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to page
driver.get("http://twitter.com/XXXXXX/media")

#You can adjust it but this works fine
SCROLL_PAUSE_TIME = 2

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height


# Now that the page is fully scrolled, grab the source code.
src = driver.page_source

#Past it into BS
soup = BeautifulSoup(src, 'html.parser')
divs = soup.find_all('div',class_='account')

#PRINT RESULT
#print('printing results')
#for div in divs:
#    print(div['data-user-id'])


#SAVE IN FILE
print('Saving results')    
with open('file.txt','w') as f:
   for div in divs:
        f.write(div['data-user-id']+'\n')   

所以程序运行良好。它检索 id 并打印它们或将它们写入 txt 文件。我现在可以将这个 id 列表粘贴到 Calc 中,并添加一个数据透视表来查看每个 id 被标记了多少次。 但!我还有一些问题:

-我只得到 id,而不是用户名。现在有什么更简单的方法:在收集 id 的同时收集用户名并将它们放在文件中?或者后期将ids文件转换成用户名文件?最后的解决方案怎么可能?

-我无法无限向下滚动。我回到了 2018 年 9 月,但仅此而已。它只是说“回到顶部”。现在,是因为我没有登录 Twitter 还是因为某些内置限制?

如果您有任何意见、想法等...任何帮助将不胜感激。 谢谢!

编辑1: 我从here 找到了这个(Tweepy)解决方案:

def get_usernames(ids):
    """ can only do lookup in steps of 100;
        so 'ids' should be a list of 100 ids
    """
    user_objs = api.lookup_users(user_ids=ids)
    for user in user_objs:
        print(user.screen_name)

所以,由于我的列表超过 100,我应该这样做:

对于更大的 id 集,您可以将其放入 for 循环并调用 因此,同时遵守 twitter API 限制。

【问题讨论】:

  • 有什么问题要问我们吗?
  • 很多。例如,我想知道如何使用逗号分隔的 id 列表而不是 id 列。然后我想将其保存为 csv 或其他格式,以便以后用作变量。

标签: python selenium twitter beautifulsoup


【解决方案1】:

您的代码没有为我生成 ID,因此最初无法测试这些解决方案。不确定问题是什么,因为我没有调查它,但似乎我的源 html 没有任何class='account'。所以我在代码中将其更改为只说“查找所有具有属性“data-user-id”的 div 标签:

 divs = soup.find_all('div', {"data-user-id" : re.compile(r".*")})

1) 要拥有 csv,您只需编写并保存为 csv,而不是 txt。另一种选择是创建一个带有 id 的数据框,然后使用 pandas 写入带有 df.to_csv('path/to/file.csv') 的 csv

2) 将其放入列表中也很容易。

创建 ID 列表 - for Loop

#TO PUT INTO LIST (FOR LOOP)
id_list = []
for div in divs:
    id_list.append(div['data-user-id'])

print (id_list)

创建 ID 列表 - 列表理解

#TO PUT INTO LIST (LIST COMPREHENSION)
id_list = [ div['data-user-id'] for div in divs ]

写入 CSV

#SAVE IN FILE
import csv
print('Saving results')    
with open('file.csv','w', newline='') as f:
    writer = csv.writer(f)
    for div in divs:
        writer.writerow([div['data-user-id']])   

【讨论】:

  • 非常感谢,我要试试!我也在寻找一种在获取数据用户 ID 的同时获取用户名的方法。完成后,将其放入 csv 中,其中一列用于 ids,另一列用于用户名。
  • 我快速浏览了一下,确实记得我在那里看到了用户名,因此将这些用户 ID 与用户名匹配应该不会太难。然后,当您将其编写为 csv 时,只需将用户 ID 和用户名都写入一行
  • 是的,我也必须获取data-screen-name,但我不知道如何使用find_all
  • 最后一件事,divs = soup.find_all('div', {"data-user-id" : re.compile(r".*")}) 在 csv 文件的开头我得到了很多重复。我认为这是因为代码中多次出现data-user-id
  • 另外还有一些奇怪的东西。一些帐户的用户 ID 很长,例如 1024596885661802496,但 csv 文件中的程序输出为 1024596885661800000。为什么会这样???编辑:如果我用记事本打开 csv 文件,数据是正确的。但如果我用 Excel 或 Calc 打开它,它就会搞砸了。
猜你喜欢
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2022-10-18
  • 2015-12-26
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多