Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

python - href with 2 different links but with same content

I wanted to create a small program using tutorials on youtube to scrape download links on a video website.

but the problem is that the 'href' that hold the download links sometimes hold the same content. In fact it's a forum, there's the link to download a video and the same video in smaller size just below. If we click on the video we are redirected to the same page that the link above redirects us.

Here is the small program:

import requests
from bs4 import BeautifulSoup

links =[]

for p in range(21):
    url = 'website url' + str(p)
    response = requests.get(url)
    print(response)

    if response.ok:
        print('Page: ' + str(p))
        soup = BeautifulSoup(response.text, "html.parser")
        iS = soup.findAll('i')

        for i in iS:
            a = i.find('a')
            link = a['href']
            links.append(link)

print(len(links))

with open('urls.txt', 'w') as file:
    for link in links:
        file.write(link + '
')


with open('urls.txt', 'r') as file:
    for row in file:
        print(row)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...