473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex single quotes in scraper script?

Hi, I started using a python based screen scraper called newsscraper I
downloaded from sourceforge.
http://sourceforge.net/projects/newsscraper/. I have created many python
templates that work just fine from their examples however I ran into a road
block with sites that use single quotes instead of double quotes for
specifying url in their web pages.

For example: <a href='http://www.foo/'>

instead of the usual
<a href="http://www.foo/">

Being a real newbie with this I think I found the area of code that parses
the href. It is in a file called parsefns.py
the full excerpt is listed below but here is the regex line that I believe
is not dealing with single quote.

m = re.search(r'href\s*=\s*"?([^>" ]+)["> ]', text, re.I)

I have tried many different variations but no luck and no luck getting hold
of the author. Any ideas? Thx.

---------------------
def get_href(text, base_url=None):
"""get_href(text[, base_url]) -> href or None

Extract the URL out of an HREF tag. If base_url is provided,
will attempt to resolve relative links.

"""
m = re.search(r'href\s*=\s*"?([^>" ]+)["> ]', text, re.I)
if not m:
return None
link = m.group(1)
if base_url and not link.lower().startswith("http"):
import urlparse
link = urlparse.urljoin(base_url, link)
return link
===============
Jul 18 '05 #1
3 2046

"Unknown" <un*****@unknown.invalid> wrote in message
news:10*************@corp.supernews.com...
Hi, I started using a python based screen scraper called newsscraper I
downloaded from sourceforge.
http://sourceforge.net/projects/newsscraper/. I have created many python templates that work just fine from their examples however I ran into a road block with sites that use single quotes instead of double quotes for
specifying url in their web pages.

For example: <a href='http://www.foo/'>

instead of the usual
<a href="http://www.foo/">

Being a real newbie with this I think I found the area of code that parses the href. It is in a file called parsefns.py
the full excerpt is listed below but here is the regex line that I believe is not dealing with single quote.

m = re.search(r'href\s*=\s*"?([^>" ]+)["> ]', text, re.I)

I have tried many different variations but no luck and no luck getting hold of the author. Any ideas? Thx.


Did you try reversing all single and double quotes? ie r"...'...'...'..."
If that doesn't work, you need someone else to answer.
A list of the variations not working might also help someone to answer.

TJR

Jul 18 '05 #2
On Fri, 16 Jul 2004, Rock wrote:
Being a real newbie with this I think I found the area of code that parses
the href. It is in a file called parsefns.py
the full excerpt is listed below but here is the regex line that I believe
is not dealing with single quote.

m = re.search(r'href\s*=\s*"?([^>" ]+)["> ]', text, re.I)

I have tried many different variations but no luck and no luck getting hold
of the author. Any ideas? Thx.


Good job tracking that down. Methinks you'll want to change it to read
thusly:

m = re.search(r'href\s*=\s*["\']?([^>"\' ]+)["\'> ]', text, re.I)

This will possibly break some sites, though (namely those that use single
quotes in their URLs, but those are broken anyways). A proper fix would
require a tad more work (i.e. either a much, much, messier regex or a
change in the function), and it's really late right now ;)

Jul 18 '05 #3

"Christopher T King" <sq******@WPI.EDU> wrote in message
news:Pi**************************************@ccc2 .wpi.edu...
On Fri, 16 Jul 2004, Rock wrote:
Being a real newbie with this I think I found the area of code that parses the href. It is in a file called parsefns.py
the full excerpt is listed below but here is the regex line that I believe is not dealing with single quote.

m = re.search(r'href\s*=\s*"?([^>" ]+)["> ]', text, re.I)

I have tried many different variations but no luck and no luck getting hold of the author. Any ideas? Thx.
Good job tracking that down. Methinks you'll want to change it to read
thusly:

m = re.search(r'href\s*=\s*["\']?([^>"\' ]+)["\'> ]', text, re.I)


woohoo! that fixed my problem with single quotes sites and double quotes
still seem to still work just fine.

Thanks man.

This will possibly break some sites, though (namely those that use single
quotes in their URLs, but those are broken anyways). A proper fix would
require a tad more work (i.e. either a much, much, messier regex or a
change in the function), and it's really late right now ;)

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
3
by: Craig Kenisston | last post by:
I have the sudden need to split a text that may have any of the following tokens : Words with quotes or double quotes. Words with no quotes at all. Numbers with and without decimal points, no...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
7
by: Fuzzyman | last post by:
Hello all, I'm writing a module that takes user input as strings and (effectively) translates them to function calls with arguments and keyword arguments.to pass a list I use a sort of 'list...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
1
by: swestenra | last post by:
I am trying to build a screen scraper. But not just a plain screen scraper, it must also automate the entry of data. Background: We have a new intranet system that goes in to production soon. ...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
5
by: Bragadiru | last post by:
Hi, I'm using the following Regex to parse for method parameters. It works if there are no spaces between commas. How can I change the regex to support method calls like : MyMethod('uno', ...
6
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.