Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Minor fix in defaultViewport option for launchPuppeteer()

Jan Curned93c1e5964c
diff --git a/src/utils_social.js b/src/utils_social.js
index a2b601cce..d4fefb018 100644
--- a/src/utils_social.js
+++ b/src/utils_social.js
@@ -6,8 +6,24 @@ import cheerio from 'cheerio';
// Regex inspired by https://zapier.com/blog/extract-links-email-phone-regex/
// eslint-disable-next-line max-len
const EMAIL_REGEX_STRING = '(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])';
+
+/**
+ * Regular expression to exactly match a single email address.
+ * It has the following form: `/^...$/i`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
const EMAIL_REGEX = new RegExp(`^${EMAIL_REGEX_STRING}$`, 'i');
+
+/**
+ * Regular expression to find multiple email addresses in a text.
+ * It has the following form: `/.../ig`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
const EMAIL_REGEX_GLOBAL = new RegExp(EMAIL_REGEX_STRING, 'ig');
+
+
const EMAIL_URL_PREFIX_REGEX = /^mailto:/i;
@@ -49,11 +65,6 @@ const emailsFromUrls = (urls) => {
};
-const LINKEDIN_URL_REGEX = /http(s)?:\/\/[a-zA-Z]+\.linkedin\.com\/in\/[a-zA-Z0-9\-_%]+/g;
-const INSTAGRAM_URL_REGEX = /(?:(^|[^0-9a-z]))(((http|https):\/\/)?((www\.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9_.]{2,30})))/ig;
-// eslint-disable-next-line max-len, no-useless-escape
-const TWITTER_URL_REGEX = /(?:(?:http|https):\/\/)?(?:www.)?(?:twitter.com)\/(?!(oauth|account|tos|privacy|signup|home|hashtag|search|login|widgets|i|settings|start|share|intent|oct)([\'\"\?\.\/]|$))([A-Za-z0-9_]{1,15})/igm;
-
// Supports URLs starting with `tel://`, `tel:/` and `tel:`, and similarly `phone` and `telephone`
const PHONE_URL_PREFIX_REGEX = /^(tel|phone|telephone):(\/)?(\/)?/i;
@@ -166,6 +177,67 @@ const phonesFromUrls = (urls) => {
};
+const LINKEDIN_URL_REGEX_STRING = '(http(s)?:\\/\\/)?([a-z]+\\.)?linkedin\\.com\\/in\\/[a-zA-Z0-9\\-_%]+';
+const INSTAGRAM_URL_REGEX_STRING = '(http(s)?:\\/\\)?([a-z]+\\.)?(instagram\\.com|instagr\\.am)\\/[a-z0-9_.]{2,30}';
+// const INSTAGRAM_URL_REGEX_STRING = '(?:(^|[^0-9a-z]))(((http|https):\\/\\/)?((www\\.)?(?:instagram.com|instagr.am)\\/([A-Za-z0-9_.]{2,30})))';
+// eslint-disable-next-line max-len, quotes
+const TWITTER_URL_REGEX_STRING = `(?:(?:http|https):\\/\\/)?(?:www.)?(?:twitter.com)\\/(?!(oauth|account|tos|privacy|signup|home|hashtag|search|login|widgets|i|settings|start|share|intent|oct)([\\'\\"\\?\\.\\/]|$))([A-Za-z0-9_]{1,15})`;
+
+// https://www.facebook.com/profile.php?id=1153222087
+// https://www.facebook.com/julianwaldthaler
+//
+
+/**
+ * Regular expression to exactly match a single LinkedIn profile URL.
+ * It has the following form: `/^...$/i`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const LINKEDIN_URL_REGEX = new RegExp(`^${LINKEDIN_URL_REGEX_STRING}$`, 'i');
+
+/**
+ * Regular expression to find multiple LinkedIn profile URLs in a text.
+ * It has the following form: `/.../ig`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const LINKEDIN_URL_REGEX_GLOBAL = new RegExp(LINKEDIN_URL_REGEX_STRING, 'ig');
+
+
+/**
+ * Regular expression to exactly match a single Instagram profile URL.
+ * It has the following form: `/^...$/i`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const INSTAGRAM_URL_REGEX = new RegExp(`^${INSTAGRAM_URL_REGEX_STRING}$`, 'i');
+
+/**
+ * Regular expression to find multiple Instagram profile URLs in a text.
+ * It has the following form: `/.../ig`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const INSTAGRAM_URL_REGEX_GLOBAL = new RegExp(INSTAGRAM_URL_REGEX_STRING, 'ig');
+
+
+/**
+ * Regular expression to exactly match a single Instagram profile URL.
+ * It has the following form: `/^...$/i`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const TWITTER_URL_REGEX = new RegExp(`^${TWITTER_URL_REGEX_STRING}$`, 'i');
+
+/**
+ * Regular expression to find multiple Instagram profile URLs in a text.
+ * It has the following form: `/.../ig`.
+ * @type {RegExp}
+ * @memberOf utils.social
+ */
+const TWITTER_URL_REGEX_GLOBAL = new RegExp(TWITTER_URL_REGEX_STRING, 'ig');
+
+
/**
* The functions attempts to extract the following social handles from a HTML document:
* emails, phones. Note that the function removes duplicates.
@@ -175,6 +247,7 @@ const phonesFromUrls = (urls) => {
* {
* emails: String[],
* phones: String[],
+ * linkedInUrls: String[],
* }
* ```
*/
@@ -182,13 +255,16 @@ const handlesFromHtml = (html) => {
const result = {
emails: [],
phones: [],
+ linkedIns: [],
+ twitters: [],
+ instagrams: [],
};
if (!_.isString(html)) return result;
// We use ignoreHref and ignoreImage options so that the text doesn't contain links,
// since their parts can be interpreted as e.g. phone numbers.
- const text = htmlToText.fromString(html, { ignoreHref: true, ignoreImage: true });
+ const text = htmlToText.fromString(html, { ignoreHref: true, ignoreImage: true }) || '';
// TODO: Both html-to-text and cheerio use htmlparser2, the parsing could be done only once to improve performance
const $ = cheerio.load(html, { decodeEntities: true });
@@ -202,19 +278,31 @@ const handlesFromHtml = (html) => {
// TODO: We should probably normalize all the handles to lower-case
result.emails = emailsFromUrls(linkUrls).concat(emailsFromText(text));
- result.emails.sort();
- result.emails = _.uniq(result.emails, true);
-
result.phones = phonesFromUrls(linkUrls).concat(phonesFromText(text));
- result.phones.sort();
- result.phones = _.uniq(result.phones, true);
+
+ // Note that these regexps extract just the base profile path. For example, for URL:
+ // https://www.linkedin.com/in/carl-newman-123456a/detail/recent-activity/
+ // they match just:
+ // https://www.linkedin.com/in/carl-newman-123456a
+ result.linkedIns = html.match(LINKEDIN_URL_REGEX_GLOBAL) || [];
+ result.twitters = html.match(TWITTER_URL_REGEX_GLOBAL) || [];
+ result.instagrams = html.match(INSTAGRAM_URL_REGEX_GLOBAL) || [];
+ // result.facebooks = html.match(INSTAGRAM_URL_REGEX_GLOBAL) || [];
+
+ // Sort and deduplicate handles
+ ['emails', 'phones', 'linkedIns', 'instagrams'].forEach((property) => {
+ result[property].sort();
+ result[property] = _.uniq(result[property], true);
+ });
return result;
};
/**
- * A namespace that contains various Puppeteer utilities.
+ * A namespace that contains various utilities to help you extract social handles
+ * lie
+ * from text, URLs and and HTML documents.
*
* **Example usage:**
*
@@ -231,4 +319,16 @@ export const socialUtils = {
phonesFromText,
phonesFromUrls,
handlesFromHtml,
+
+ EMAIL_REGEX,
+ EMAIL_REGEX_GLOBAL,
+
+ LINKEDIN_URL_REGEX,
+ LINKEDIN_URL_REGEX_GLOBAL,
+
+ INSTAGRAM_URL_REGEX,
+ INSTAGRAM_URL_REGEX_GLOBAL,
+
+ TWITTER_URL_REGEX,
+ TWITTER_URL_REGEX_GLOBAL,
};
diff --git a/test/utils_social.js b/test/utils_social.js
index bf45c13f6..1353c6efb 100644
--- a/test/utils_social.js
+++ b/test/utils_social.js
@@ -1,3 +1,4 @@
+import _ from 'underscore';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
// import * as utils from '../build/utils';
@@ -302,13 +303,13 @@ describe('utils.social.phonesFromUrls()', () => {
});
});
-
-
-
describe('utils.social.handlesFromHtml()', () => {
const EMPTY_RESULT = {
emails: [],
phones: [],
+ linkedIns: [],
+ instagrams: [],
+ twitters: [],
};
it('handles invalid arg', () => {
@@ -329,25 +330,77 @@ describe('utils.social.handlesFromHtml()', () => {
<title>Bla</title>
</head>
<a>
+ [email protected] testing skipping duplicates xxx@blabla
<p>carl&#64;example.com</p>
<a href="mailto:[email protected]"></a>
<a href="mailto:david&#64;example.com"></a>
- <a href="[email protected]"></a>
+ <a href="[email protected]"></a>
<img src="http://somewhere.com/ [email protected] " />
<a href="http://somewhere.com/ [email protected] "></a>
- &#43;420775222222
+ &#43;420775222222
+4207751111111
+ +4207751111111 test duplicate
+ 1 2 3 4 5 6 7 8 9 0
<a href="skip.this.one: +42099999999"></a>
<a href="tel://+42077533333"></a>
+ https://www.linkedin.com/in/bobnewman/
+ https://www.linkedin.com/in/alicenewman/
+ https://www.linkedin.com/in/alicenewman/ duplicate
+ http://www.linkedin.com/in/nohttps/
+ https://cz.linkedin.com/in/somecountry
+ https://www.linkedin.com/in/carl-newman/ignored-sub-link
+ https://www.linkedin.com/in/first-last-123456a
+ <a href="https://www.linkedin.com/in/jancurn">Profile</a>
+ <a href="https://www.linkedin.com/in/carl-newman-5555555a/detail/recent-activity/">Sub-link</a>
+
+ https://www.instagram.com/old_prague/
+ https://www.instagram.com/old_prague
+
+ https://www.instagram.com/newyorkarea/
+
+ <a href="https://www.instagram.com/york">link</a>
+
</body>
</html>
`)).to.eql({
phones: ['+4207751111111', '+420775222222', '+42077533333'],
+ linkedIns: [
+ 'http://www.linkedin.com/in/nohttps',
+ 'https://cz.linkedin.com/in/somecountry',
+ 'https://www.linkedin.com/in/alicenewman',
+ 'https://www.linkedin.com/in/bobnewman',
+ 'https://www.linkedin.com/in/carl-newman',
+ 'https://www.linkedin.com/in/carl-newman-5555555a',
+ 'https://www.linkedin.com/in/first-last-123456a',
+ 'https://www.linkedin.com/in/jancurn',
+ ],
+ instagrams: [
+ 'https://www.instagram.com/newyorkarea',
+ 'https://www.instagram.com/old_prague',
+ 'https://www.instagram.com/york',
+ ],
+ twitters: [],
});
});
});
+
+describe('utils.social REGEXPES', () => {
+ it('exist', () => {
+ expect(_.isRegExp(social.EMAIL_REGEX)).to.eql(true);
+ expect(_.isRegExp(social.EMAIL_REGEX_GLOBAL)).to.eql(true);
+
+ expect(_.isRegExp(social.LINKEDIN_URL_REGEX)).to.eql(true);
+ expect(_.isRegExp(social.LINKEDIN_URL_REGEX_GLOBAL)).to.eql(true);
+
+ expect(_.isRegExp(social.INSTAGRAM_URL_REGEX)).to.eql(true);
+ expect(_.isRegExp(social.INSTAGRAM_URL_REGEX_GLOBAL)).to.eql(true);
+
+ expect(_.isRegExp(social.TWITTER_URL_REGEX)).to.eql(true);
+ expect(_.isRegExp(social.TWITTER_URL_REGEX_GLOBAL)).to.eql(true);
+ });
+});