【问题标题】:Function for these parse elements will do not repeat. BeautifulSoup这些解析元素的功能将不再重复。美丽汤
【发布时间】:2019-10-23 18:25:52
【问题描述】:

哪个函数(或其他)是理想的,这样这些昵称就不会在我的解析器上重复。不知道该怎么做。如果您能帮助我,我将不胜感激。

来源:

from urllib.request import urlopen as uReq
from urllib.request import Request
from bs4 import BeautifulSoup as soup

# save all the nicknames to 'CSV' file format
filename = "BattlePassNicknames.csv"
f = open(filename, "a", encoding="utf-8")
headers1 = "Member of JAZE Battle Pass 2019\n"
b = 1
if b < 2:
    f.write(headers1)
    b += 1
# start page
i = 1
while True:
    # disable jaze guard. turn off html 'mod_security'
    link = 'https://jaze.ru/forum/topic?id=50&page='+str(i)
    my_url = Request(
        link,
        headers={'User-Agent': 'Mozilla/5.0'}
    )
    i += 1  # increment page no for next run
    uClient = uReq(my_url)
    if uClient.url != link:
        break
    page_html = uClient.read()
    # Check if there was a redirect
    uClient.close()
    # html parsing
    page_soup = soup(page_html, "html.parser")
    # grabs each name of player
    containers = page_soup.findAll("div", {"class": "top-area"})

    for container in containers:
        playerName = container.div.a.text.strip()
        print("BattlePass PlayerName: " + playerName)
        f.write(playerName + "\n")

【问题讨论】:

    标签: python-3.x parsing beautifulsoup


    【解决方案1】:

    您可以将所有名称添加到set。

    集合对象是不同的可散列对象的无序集合。常见用途包括成员资格测试、从序列中删除重复项以及计算数学运算,例如交集、并集、差异和对称差异。

    my_set = set()
    # Lets add some elements to a set
    my_set.add('a')
    my_set.add('b')
    print(my_set)  # prints {'a', 'b'}
    # Add one more 'a'
    my_set.add('a')
    print(my_set) # still prints {'a', 'b'} !
    

    在您的情况下,让我们将所有名称添加到一个集合中,然后在 for 循环之后写入文件。

    from urllib.request import urlopen as uReq
    from urllib.request import Request
    from bs4 import BeautifulSoup as soup
    
    # save all the nicknames to 'CSV' file format
    filename = "BattlePassNicknames.csv"
    f = open(filename, "a", encoding="utf-8")
    headers1 = "Member of JAZE Battle Pass 2019\n"
    b = 1
    if b < 2:
        f.write(headers1)
        b += 1
    # start page
    i = 1
    names = set()
    while True:
        # disable jaze guard. turn off html 'mod_security'
        link = 'https://jaze.ru/forum/topic?id=50&page='+str(i)
        my_url = Request(
            link,
            headers={'User-Agent': 'Mozilla/5.0'}
        )
        i += 1  # increment page no for next run
        uClient = uReq(my_url)
        if uClient.url != link:
            break
        page_html = uClient.read()
        # Check if there was a redirect
        uClient.close()
        # html parsing
        page_soup = soup(page_html, "html.parser")
        # grabs each name of player
        containers = page_soup.findAll("div", {"class": "top-area"})
    
        for container in containers:
            playerName = container.div.a.text.strip()
            names.add(playerName)
    
    for name in names:
        f.write(name + "\n")
    f.close()
    

    编辑

    集合不保留顺序。如果您想保留订单,只需使用列表即可。

    ...
    names = []
    while True:
    ...
        for container in containers:
            playerName = container.div.a.text.strip()
            if playerName not in names:
                names.append(playerName)
    
    for name in names:
        f.write(name + "\n")
    f.close()
    

    【讨论】:

    • 它有效,谢谢!但是他把所有的名字都混在一起了,这个第一名的家伙已经到了44位。为什么以及如何解决?
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    相关资源
    最近更新 更多