0

I would like to scrape all the links that end with .php I have written a regrex to select the target url such as samsung-phones-f-9-0-r1-p1.php

I am wondering if there's something wrong with my regrex or the tag is not correct.

Thank you so much in advance for answering

from bs4 import BeautifulSoup
import urllib.request as urlopen
import ssl 
import re

base_url = 'https://www.gsmarena.com/samsung-phones-9.php'
webrequest = request.Request(url, headers = {
    "User-Agent" : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36'})
    
    
# open the url
html = request.urlopen(base_url).read().decode('utf-8')
soup = BeautifulSoup(html, features = 'lxml')
# scraping sub urls
sub_urls = soup.find_all('a', {"href": re.compile("(samsung).+(.php)")})
# https:\/\/www\.gsmarena\.com\/samsung.+(.php)
print(sub_urls)

The wrong result is demonstrated in bellow

1
  • What is the expected output? Commented Dec 25, 2020 at 20:17

2 Answers 2

1

You are doing it right but you are not extracting the actual href property from the tags.
Modify this line:

sub_urls = soup.find_all('a', {"href": re.compile("(samsung).+(.php)")})

to this:

sub_urls = [x.get('href') for x in soup.find_all('a', {"href": re.compile("(samsung).+(.php)")})]
Sign up to request clarification or add additional context in comments.

2 Comments

Could I ask why it needs a get() to return href? I thought if I specified "href" and all the "href" tags would be selected.
anything specified within find_all() function would help narrow down the tag selection. this is why your find_all function was returning list of <a> tags. To get any attribute of selected tags, you must use .get() function and specify the key you need.
0

Your regular expression is not exactly wrong, since it will capture the URLs you are aiming for. However, two points to consider: (1) the parenthesis are unnecessary and (2) you should escape the . character in .php, or it'll be compiled as a quantifier. A better solution might look like this: samsung.+\.php. Of course this is will only capture the .php file itself and not the whole URL. If wanted the whole thing you'd have to use .*samsung.+\.php.
You can always use an online tool to test your regular expressions.

Either way, you haven't outlined any concrete issues, therefore I'm not sure if I've satisfied your doubts.

1 Comment

I have tried the online tool that shows the paired result is okay, I suspect the problem is html tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.